多维数组
public static void main(String[] args) { //静态初始化 多维数组 int[][] arr1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; System.out.println(arr1);//不转 System.out.println(Arrays.toString(arr1));//之前的方式 System.out.println(Arrays.deepToString(arr1));//多维数组转化 //动态初始化 int[][] ints = new int[5][2]; //外层5个 内层2个 数据5*2=10 for (int i = 0; i < ints.length; i++) { for (int j = 0; j < ints[i].length; j++) { //(Math.random() *100) 随机数 1-99 的范围 ints[i][j] = (int) (Math.random() * 100); System.out.print(ints[i][j] + "\t"); } } //System.out.print("\t"); System.out.println(Arrays.deepToString(ints)); //遍历数组 for (int i = 0; i < arr1.length; i++) { for (int j = 0; j < arr1[i].length; j++) { System.out.print(arr1[i][j] + "\t"); } System.out.println(); } //本质 一维数组 外层是3层 里层不断递增 int[][] ints1 = new int[3][]; ints1[0] = new int[]{1,2}; ints1[1] = new int[]{3,4,5}; ints1[2] = new int[]{6,7,8,9}; System.out.println(Arrays.deepToString(ints1)); //更多的维度数组 int[][][] arr3={ {{11,12}},{{13,14}},{{15,16}} }; System.out.println(Arrays.deepToString(arr3));
代码运行结果是
[[I@5a39699c [[I@3cb5cdba, [I@56cbfb61, [I@1134affc] [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 83 36 76 96 61 86 31 46 64 7 [[83, 36], [76, 96], [61, 86], [31, 46], [64, 7]] 1 2 3 4 5 6 7 8 9 [[1, 2], [3, 4, 5], [6, 7, 8, 9]] [[[11, 12]], [[13, 14]], [[15, 16]]]
数组倒转
public static void main(String[] args) { //构造数组 int[] arr= new int[5]; for (int i = 0; i < arr.length; i++) { arr[i]= (int) (Math.random() * 100); } System.out.println(Arrays.toString(arr)); //数组倒转 //第一个和最后一个倒转 以此类推 //第一个 arr[0] 最后一个 arr[length-1-0] //arr[1] arr[length-1-1] //arr[2] arr[length-1-2] for (int i = 0; i < arr.length/2; i++) { int temp=arr[i]; arr[i] =arr[arr.length-1-i]; arr[arr.length-1-i]=temp; } System.out.println(Arrays.toString(arr)); }
运行结果是
[64, 24, 78, 34, 39] [39, 34, 78, 24, 64]
求平均数
public static void main(String[] args) { //初始化数组 int[] nums = new int[5]; int index = 0; int sum = 0; int grade = 1; double avg = 0; //记录输入 Scanner scan = new Scanner(System.in); System.out.println("请输入数字,负数结束流程"); while (true){ int temp = scan.nextInt(); if (temp < 0){ break; } nums[index] = temp; sum = sum + temp; avg = (double) sum / grade; grade = grade +1; if (++index > nums.length - 1){ nums = Arrays.copyOf(nums,nums.length+5); } } scan.close(); // System.out.println(Arrays.toString(nums)); // System.out.println(sum); System.out.println("平均数是="+avg); for (int i = 0 ;i < nums.length;i++){ if (nums[i] > avg){ System.out.print("比平均数大的值是="+nums[i]+"\t"); } } }
运行结果是
请输入数字 65 36 59 2365 5 4 1 8 56 -4 平均数是288.77777777777777 比平均数大的值是2365
冒泡排序
public static void main(String[] args) { //冒泡排序 构造数组 int[] ints = new int[8]; for (int i = 0; i < ints.length - 1; i++) { ints[i] = (int) (Math.random() * 100); } System.out.println("初始默认值是"+Arrays.toString(ints)); //轮次 for (int i = 0; i < ints.length - 1; i++) { //比较的次数 // 1次: 下标 0 -- ints.length-1 // 下标: 1 -- ints.length-1 类推 //循环的边界 //j=ints.length-1-i-1 //j+1=ints.length--1-i for (int j = 0; j < ints.length-1-i-1; j++) { //从大到小 if (ints[j] < ints[j+1]) { int temp=ints[j]; ints[j]=ints[j+1]; ints[j+1]=temp; } } //打印完整的排序过程 System.out.println(Arrays.toString(ints)); } }
代码运行结果是
[40, 59, 96, 30, 16, 40, 45, 0] [59, 96, 40, 30, 40, 45, 16, 0] [96, 59, 40, 40, 45, 30, 16, 0] [96, 59, 40, 45, 40, 30, 16, 0] [96, 59, 45, 40, 40, 30, 16, 0] [96, 59, 45, 40, 40, 30, 16, 0] [96, 59, 45, 40, 40, 30, 16, 0] [96, 59, 45, 40, 40, 30, 16, 0]