方式有很多種,
1.二維數(shù)組的方式
2.Vector(不定長(zhǎng)數(shù)組)嵌套的方式
3.Vector內(nèi)套數(shù)組
3.List>的方式
4.List>的方式
……
下面我用兩種方式給你一個(gè)簡(jiǎn)單的demo:
1.二維數(shù)組
// 創(chuàng)建定長(zhǎng)二維數(shù)組
public static int[][] getArrays(){
// 創(chuàng)建二維數(shù)組,2 -- 表示外層數(shù)組的長(zhǎng)度,3表示里層數(shù)組的長(zhǎng)度
int[][] a = new int[2][3];
int[] b = {1, 2, 3};
int[] c = {4, 5, 6};
a[0] = b;
a[1] = c;
return a;
}
// 二維數(shù)組
int[][] a = getArrays();
for(int i = 0; i < a.length; i++){
int[] b = a[i];
for(int j = 0; j < b.length; j++){
System.out.print(b[j] + " ");
}
System.out.println(" ");
}
System.out.println("\n\n");
2.Vector內(nèi)套數(shù)組的方式
// 外層使用不定長(zhǎng)數(shù)組
public static Vector<int[]> getVectors(){
Vector<int[]> vector = new Vector<>();
int[] b = {1, 2, 3};
int[] c = {4, 5, 6};
vector.add(b);
vector.add(c);
return vector;
}
// vector
Vector<int[]> vector = getVectors();
// 獲取迭代器
Iterator<int[]> iterator = vector.iterator();
while (iterator.hasNext()) {
int[] b = iterator.next();
for(int c : b){
System.out.print(c + " ");
}
System.out.println(" ");
}