二维数组的声明与初始化:
class ArrayTest3
{
public static void main(String[] args)
{
//1.声明一个二维数组
int[][] arr;
//2.为二维数组进行初始化
//①静态初始化:初始化操作和赋值操作同时进行
arr=new int[][]{{1,2,3},{4,5,6},{7,8,9}};
//②动态初始化
//方式一:
arr=new int[5][6];//{{0,0,0,0,0,0},{0,0,0,22,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0}}
arr[1][3]=22;
/*//方式二:
arr=new int[5][];//{{0},{0,0},null,{0,0,11,0},null}
arr[0]=new int[1];
arr[1]=new int[2];
arr[3]=new int[4];
arr[3][2]=11;*/
//3.使用循环遍历二维数组
/*for (int i=0;i { int as[]=arr[i]; for (int j=0;j { System.out.print(as[j]+"\t"); } System.out.println(); } */
for (int i=0;i { for (int j=0;j { System.out.print(arr[i][j]+"\t"); } System.out.println(); } for (int[] as:arr) { for (int a:as) { System.out.print(a+"\t"); } System.out.println(); } } } 二维数组的遍历: 优化: