java新手笔记7 找最小、最大、排序
1.最小数
// 1.0
public class SortDemo1 { public static void main(String[] args) { int[] a = {2,5,4,6,8,1,3,9};
int min = a[0];//最小数
int k = 0;
int temp;
for(int i = 0; i < a.length; i++ ) {
System.out.print( a[i] + "\t");
}
//最小数 for(int j = 0; j < a.length; j++ ) {
if( min > a[j]) {
k = j;
min = a[j];
} } temp = a[0];
a[0] = a[k];
a[k] = temp; System.out.println("\n最小值 : " + min);
System.out.println("索引 : " + k);
System.out.println("a[5] : " + a[k]);
System.out.println("a[0] : " + a[0]); for(int i = 0; i < a.length; i++ ) {
System.out.print( a[i] + "\t"); } } }
2.数组最小数
// 1.1
public class SortDemo2 { public static void main(String[] args) { int[] a = {0,2,5,2,5,0};
int min = a[0];//最小数
int k = 0;
int temp;
System.out.print( "数组数据 : ");
for(int i = 0; i < a.length; i++ ) {
System.out.print( a[i] + "\t");
}
//最小数 for(int i = 0; i < a.length - 1; i++ ) {
k = i; //默认当前的最小值
//从剩下的元素找最小值
for(int j = i; j < a.length; j++ ) {//内层的起始值
if( a[k] > a[j]) {//a[k]值是默认最小值
k = j;
}
} //数据交换 如果当前值最小 不进行交换
if(k != i) {
temp = a[i];// 0 0 k == i
a[i] = a[k];
// System.out.print(".temp = " + temp + " : a[k] = " + a[k] + " : a[i] = " + a[i]);
a[k] = temp;
}
/*
System.out.print( "\n");
for(int x = 0; x < a.length; x++ ) {
System.out.print( a[x] + "\t");
}
*/
}
System.out.print( "\n");
System.out.print( "数组数据 : ");
for(int i = 0; i < a.length; i++ ) {
System.out.print( a[i] + "\t"); } } }
3.排序,大数后置
// 1.1
public class SortDemo3 { public static void main(String[] args) { int[] a = {7,5,6,3,2,1,4,9,8,6}; int temp;
System.out.print( "数组数据 : ");
for(int i = 0; i < a.length; i++ ) {
System.out.print( a[i] + "\t");
}
//相邻的元素比较 8
for(int i = 0; i < a.length - 1; i++ ) {
//每次找最大数放在最后
for(int j = 0; j < a.length - 1 - i; j++ ) { // 0 - 3
if( a[j] > a[j+1] ) { //交换
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
} }
System.out.print( "\n");
System.out.print( "数组数据 : ");
for(int i = 0; i < a.length; i++ ) {
System.out.print( a[i] + "\t"); } } }
java新手笔记7 找最小、最大、排序的更多相关文章
- java新手笔记34 连接数据库
1.JdbcUtil package com.yfs.javase.jdbc; import java.sql.Connection; import java.sql.DriverManager; i ...
- java新手笔记31 集合实现类
Person类: package com.yfs.javase; import java.util.Date; public class Person implements Comparable { ...
- java新手笔记10 构造器
1.摇奖小程序 package com.yfs.javase; import java.io.IOException; import java.nio.CharBuffer; import java. ...
- java新手笔记1 Hello World!
//Hello.java文件 //类声明 public class Hello{ //声明方法 main程序入口 public static void main (String[] args) { S ...
- JAVA新手笔记 Intent对象和Bundle对象
Intent对象和Bundle对象 功能主要是在 MainActivity中定义了2个EditText,当用户输入内容,把他传入到第二个活动, 自己新创的活动中,MyActivity中 放在MainA ...
- java新手笔记33 多线程、客户端、服务器
1.Mouse package com.yfs.javase; public class Mouse { private int index = 1; private boolean isLive = ...
- java新手笔记32 jdk5新特性
1.for package com.yfs.javase; import java.awt.Color; import java.util.Calendar; import java.util.Has ...
- java新手笔记30 集合
1.set/list package com.yfs.javase; import java.util.ArrayList; import java.util.Collection; import j ...
- java新手笔记29 读取文件
1.读取文件 package com.yfs.javase; import java.io.FileInputStream; import java.io.FileReader; import jav ...
随机推荐
- HW4.46
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- Pascal <-> C/C++ 转换简明教程
在OI的学习中,阅读多种语言的能力是很重要的. 本文就着眼于让大家最快地掌握这两种语言间的转换方法. 程序主结构 C/C++ Pascal 主程序(即开始执行的程序段)和其他函数.过程本质上都属于函数 ...
- 常用apt cli
常用的APT命令参数: apt-cache search package 搜索包 apt-cache show package 获取包的相关信息,如说明.大小.版本等 sudo apt-get ...
- Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Discuz!
Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Disc ...
- 在C#中生成唯一的字符串和数字【GUID】转
转自:http://www.cnblogs.com/lcwzj/archive/2009/04/16/1436992.html 当我们想要获得一个唯一的key的时候,通常会想到GUID.这个key非常 ...
- solrj6.2异常--Expected mime type application/octet-stream but got text/html.
org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://19 ...
- android 数据库的升级与降级解决方案
在我们写的程序中不避免的需要升级.或者说需要改变业务逻辑,那这个时候就需要修改数据库的字段,来适应不同的表结构 我们一般写数据库都是操作一个继承至 SQLiteOpenHelper 的类 然后我们需要 ...
- hdu2157之矩阵快速幂
How many ways?? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- cocos2d-x 屏幕适配新解
转自:http://blog.leafsoar.com/archives/2013/05-10-19.html 为了适应移动终端的各种分辨率大小,各种屏幕宽高比,在 cocos2d-x(当前稳定版:2 ...
- pair的例子
11.12 编写程序,读入string和int的序列,将每个string和int存入一个pair中,pair保存在一个vector中. #include<iostream> #includ ...