eg1
import java.util.Arrays;
public class Fur05 {
public static void main(String[] args) {
// int[] num= {1,67,18,28,5,6,26,8,9,34};
int[] num1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int target = 2;
// 快速排序法
// Arrays.sort(num);
// System.out.println(Arrays.toString(num));
// 二分法
int index = Arrays.binarySearch(num1, target);
System.out.println(Arrays.toString(num1));
}
}
eg2
import java.util.Arrays;
public class Fur06 {
public static void main(String[] args) {
// 无序数组
int[] num = {2, 7, 1, 14, 8, 12, 3, 10 };
System.out.println(Arrays.toString(num));
// n个数字来排序
// 俩俩比较靠前
// 总共比较n-1轮
// 比较n-1轮
int count=0;
for (int i = 0, n = num.length; i < num.length - 1; i++) {
//是否已经处于”有序“状态
//true代表有序
//false代表无序
boolean isSorted=true;
//每轮比较n-1-i
for (int k = 0; k < n - 1 - i; k++) {
count++;
//相邻比较
if (num[k] > num[k + 1]) {
//交换
num[k] = num[k] ^ num[k + 1];
num[k + 1] = num[k] ^ num[k + 1];
num[k] = num[k] ^ num[k + 1];
isSorted=false;
}
}
if(isSorted) {
break;
}
}
System.out.println("比较了:"+count+"次");
System.out.println(Arrays.toString(num));
}
}
eg3
import java.util.Arrays;
public class Fur07 {
public static void main(String[] args) {
String[] name= {"tina","alice","bill","mary","divid","helen",};
System.out.println(Arrays.toString(name));
//数组相邻元素比较
//a.compareTo()
//ab结果是正数
//a=b结果是零
for(int i=0,n=name.length;i0) {
String temp=name[k];
name[k]=name[k+1];
name[k+1]=temp;
}
}
}
System.out.println(Arrays.toString(name));
}
}
eg4
import java.util.Arrays;
public class Fur08 {
public static void main(String[] args) {
int[] num= {1,2,3,4,5,6,7,8,9};
//顺序
for(int i=0;i=0;i--) {
System.out.println(num[i]+"\t");
}
System.out.println();
//foreach
//只能顺序遍历
//不能修改数组
for(int n:num) {
n=n*10;
System.out.println(n+"\t");
}
System.out.println(Arrays.toString(num));
}
}