二维数组:
int arr[][] = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
1、遍历二维数组
public class demo01 { public static void main(String[] args) { //静态初始化 int arr[][] = new int[][]{{1,2,3},{4,5,6},{7,8,9}}; int i,j; //遍历二维数组 for(i=0;i<arr.length;i++){ for(j=0;j<arr[i].length;j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } } }
二、给数组赋值并输出
public class demo02 { public static void main(String[] args) { int arr[][] = new int[3][3]; arr[2][2] = 2; for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } } }
三、冒泡排序
public class demo03 { public static void main(String[] args) { //冒泡法 int arr[] = new int[]{4,21,0,-12,-3}; int temp; for(int i=0;i<arr.length-1;i++){ for(int j=0;j<arr.length-i-1;j++){ if(arr[j]>arr[j+1]){ temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } for(int num:arr){ System.out.print(num+" "); } } }