關於多維數組的總結
阿新 • • 發佈:2017-09-18
方法 復制 out pri font 結果 enter obj family
多維數組
二維數組的聲明:
Int [][]a = { {1} , {1,2} , {1,2,3} };
或:
Int [][]a = new int [2][];
a[0] = {1,2,3};
a[1] = {4,5,6};
多維數組的叠代:
int [][] b1 = new int [][]{{1,2},{3,4}};
for(int i =0; i<b1.length; i++){
for(int j =0; j<b1[i].length; j++){
System.out.print(b1[i][j]);
}
}
數組拷貝的靜態方法:
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
從指定源數組中復制一個數組,復制從指定的位置開始,到目標數組的指定位置結束。
例題:
int[] a={10,100,1000}; //定義數組a
int[] b={20,200,2000,20000}; //定義數組b
System.arraycopy(a, 1, b, 2, 2); //從a的第2位(1+1)開始拷貝,步長 為2,到 b的第3位(2+1),其余溢出
for(int x:b){
System.out.println(x); //輸出結果為20 200 100 1000
}
數組的優缺點:
優點:越界檢查
缺點:長度一經定義,不能更改。
關於多維數組的總結