示例代码一:
package com; public class ArrayDemo1 { public static void main(String[] args) { int[] arr = new int[] { 12, 34, 54, 23 }; // int[] arr = {12,34,54,23}; /* System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); System.out.println(arr[3]);*/ for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } System.out.println("---------------------"); boolean[] bs = new boolean[4]; System.out.println(bs[0]); int[] arrs = new int[4]; for(int i=0;i<arrs.length;i++){ System.out.println(arrs[i]); } } }
示例代码二:
package com; public class ArrayDemo2 { public static void main(String[] args) { int[] arr = null; arr[0] = 12; arr[1] = 13; arr[2] = 14; arr[3] = 15; arr[4] = 16; arr[5] = 16; for(int i=0;i<arr.length;i++){ System.out.println("number="+arr[i]); } } }
示例代码三:
package com; public class ArrayDemo3 { public static void main(String[] args) { int[][] arr = new int[3][2]; arr[0][0] = 1; arr[1][0] = 2; arr[2][0] = 3; arr[0][1] = 2; arr[1][1] = 2; arr[2][1] = 2; //二维数组中有3个一维数组 每个数组都默认初始值null int[][] arr2 = new int[3][]; arr2[0] = new int[3]; arr2[0][0] = 1; arr2[0][1] = 2; arr2[0][2] = 3; arr2[1] = new int[3]; arr2[1][0] = 4; arr2[1][1] = 5; arr2[1][2] = 6; arr2[2] = new int[3]; arr2[2][0] = 4; arr2[2][1] = 5; arr2[2][2] = 6; int[][] arr3 = {{12,12,32},{322,435,65,},{12,324,345}}; for(int x=0;x<arr3.length;x++){ for(int y =0;y<arr3[x].length;y++){ System.out.println(arr3[x][y]); } } } }
示例代码四:
package com; public class ArrayDemo4 { /** * 获取最大值 * @param arr * @return */ public static int getMax(int[] arr){ int max = arr[0]; for(int i=0;i<arr.length;i++){ if(arr[i]>max){ max = arr[i]; } } return max; } /** * 最大值 * @param arr * @return */ public static int getMax2(int[] arr){ int max = 0; for(int i=1;i<arr.length;i++){ if(arr[i]>arr[max]){ max = i; } } return arr[max]; } /** * 最小值 * @return */ public static int getMin(int[] arr){ int min = 0; for(int i=0;i<arr.length;i++){ if(arr[i]<arr[min]){ min = i; } } return arr[min]; } public static void main(String[] args) { //获取数组的中最大值和最小值 int[] arr = {4,5,3,1,8,6,7}; int max = ArrayDemo4.getMax(arr); System.out.println("最大值:"+max); int min = ArrayDemo4.getMin(arr); System.out.println("最小值:"+min); } }
示例代码五:
package com; public class ArrayDemo5 { public static void main(String[] args) { //对给定的数据排序 int[] arr = {4,5,3,1,8,6,7}; //选择排序 for(int x = 0 ;x<arr.length;x++){ for(int y=x+1;y<arr.length;y++){ if(arr[x]>arr[y]){ int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } } } System.out.println("排序过后:"); for(int i=0;i<arr.length;i++){ System.out.println(arr[i]); } } }
示例代码六:
package com; public class ArrayDemo6 { public static void main(String[] args) { //对给定的数据排序 int[] arr = {4,5,3,1,8,6,7}; // 5 4 3 1 8 6 7 // 5 4 3 1 8 6 7 // 5 4 3 8 1 6 7 // 5 4 3 8 6 1 7 // 5 4 3 8 6 7 1 // 5 4 8 6 7 3 1 //冒泡 for(int x=0;x<arr.length;x++){ for(int y = 0;y<arr.length-x-1;y++){//-x 让每一次比较的元素减少,-1 避免数组越界 if(arr[y]<arr[y+1]){ int temp = arr[y]; arr[y] = arr[y+1]; arr[y+1] = temp; } } } System.out.println("排序过后:"); for(int i=0;i<arr.length;i++){ System.out.println(arr[i]); } } }
时间: 2024-09-30 09:49:48