隨堂記---二維數組的定義及遍歷

2019/10/31 14:34:15

二維數組

(1)二維表數組定義

聲明:
int [][] arrInt=new int[3][3];

(2) 數組的數組

聲明:

float [][] arrFloat=new float[3][];
arrFloat[0]=new float[3]; 
arrFloat[1]=new float[4];
arrFloat[2]=new float[2]; 

(3)遍歷數組

遍歷數組:增強的for循環(for…each)
直接輸出的碼值即是一個數組地址
得到數組長度 數組名.length

二維數組,兩種遍歷書寫方式

int [][] arrInt=new int[3][3];  
int  arrInt [][]={{2,3,6},{4,6,3},{4,6,4}};  

for循環:

for(int i=0;i<arrInt.length;i++) {  
		for(int j=0;j<arrInt[i].length;j++) {  
			System.out.print(arrInt[i][j]+"  ");
		}	
		System.out.println();
	}  

增強的for循環:

	for (int[] is : arrInt) {
	   for (int i : is) {
		System.out.print(i+"   ");
	}
	   System.out.println();	
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章