我在使用arraylist创建动态二维数组时发现了一些问题.原始代码读取繁琐冗长,所以我在这里给出一个简单的代码,在这两种情况下问题都是一样的:
import java.util.*;
class test{
public static void main(String args[]){
Integer test[]=new Integer[3];
ArrayList<Integer[]> al=new ArrayList<Integer[]>();
int i,t;
test[0]=1;
test[1]=2;
test[2]=3;
al.add(test);
test[0]=4;
test[1]=5;
test[2]=6;
al.add(test);
test[0]=7;
test[1]=8;
test[2]=9;
al.add(test);
test[0]=10;
test[1]=11;
test[2]=12;
al.add(test);
Integer table[][]=new Integer[al.size()][];
table=al.toArray(table);
for(i=0;i<=al.size()-1;i++){
for(t=0;t<3;t++){
System.out.print(" "+i+" "+t+" ");
System.out.print(" "+table[i][t]+" ");}
System.out.println();
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
0 0 10 0 1 11 0 2 12
1 0 10 1 1 11 1 2 12
2 0 10 2 1 11 2 2 12
3 …Run Code Online (Sandbox Code Playgroud)