数组
public static void main(String[] args) { //静态初始化 int[] nee={88,66,456,482,62,658}; System.out.println(nee[0]+"\t"); System.out.println(nee[1]+"\t"); System.out.println(nee[2]+"\t"); System.out.println(nee[2]+"==="); nee[2]=666; System.out.println(nee[2]+"==="); System.out.println("=================>"); //动态的初始化 int[] ints = new int[7]; //[1-33] for (int i = 0; i < 6; i++) { ints[i] = (int) (Math.random() * 40 + 1); } System.out.println(ints[0]+"\t"); System.out.println(ints[1]+"\t"); System.out.println(ints[2]+"\t"); System.out.println(ints[2]+"==="); ints[2]=666; System.out.println(ints[2]+"==="); }
运行的结果是
88 66 456 456=== 666=== =================> 18 40 34 34=== 666===
遍历的数组
public static void main(String[] args) { int[] num = {7, 56, 1, 62, 52, 32}; // for循环 int max = num[0]; for (int i = 1; i < num.length; i++) { if (num[i] > max) { max = num[i]; } } System.out.println("max的最大值是多少" + max); //foreach循环 int min = num[0]; for (int i : num) { if (i < min) { min = i; } } System.out.println("max的最大值是多少"+min); }
运行结果是
max的最大值是多少62 max的最大值是多少1
Arrays工具类
public static void main(String[] args) { //1.打印数组 int[] nte={54,66,25,63,21,35,47}; System.out.println("1==>"+nte); System.out.println("2==>"+ Arrays.toString(nte)); //2.比较数组 int[] nte1={10,20,30,40,50,60,70}; int[] nte2={10,20,30,40,50,60,70}; System.out.println(nte1==nte); System.out.println(nte1==nte2); System.out.println(Arrays.equals(nte1,nte2)); //3.排序数组 int[] nte3={50,10,30,20,40,60,70}; Arrays.sort(nte3); System.out.println(Arrays.toString(nte3)); //4.复制数组 int[] nte4={50,10,30,20,40,60,70}; int[] ints = Arrays.copyOf(nte4, 5); int[] int1 = Arrays.copyOf(nte4, 8); System.out.println(Arrays.toString(ints)); System.out.println(Arrays.toString(int1)); //5.数组 int[] nte5={50,10,30,20,40,60,70}; nte5 = Arrays.copyOf(nte5, nte5.length + 1); nte5[nte5.length-1]= (int) (Math.random() *16+1); System.out.println(Arrays.toString(nte5)); }
代码的运行结果是
1==>[I@6fadae5d 2==>[54, 66, 25, 63, 21, 35, 47] false false true [10, 20, 30, 40, 50, 60, 70] [50, 10, 30, 20, 40] [50, 10, 30, 20, 40, 60, 70, 0] [50, 10, 30, 20, 40, 60, 70, 15]
内存中的数组
public static void main(String[] args) { int[] arr1={10,20,30,40,50}; int[] arr2=arr1; System.out.println(Arrays.toString(arr1)); System.out.println(Arrays.toString(arr2)); arr1[0]=111; arr2[1]=222; System.out.println(Arrays.toString(arr1)); System.out.println(Arrays.toString(arr2)); }
运行结果是
[10, 20, 30, 40, 50] [10, 20, 30, 40, 50] [111, 222, 30, 40, 50] [111, 222, 30, 40, 50]