有没有办法在java中使用循环创建可变数量的数组?

And*_*rew 3 java variables loops creation dynamic

我想知道是否有一种方法可以基于可变数量在java中创建数组.因此,如果我需要创建10个数组,则循环将生成10个(所有顺序命名).但是如果我不需要10个数组,那么循环就可以根据需要创建多个数组.

我想象的是这样的:

for(i=0 up to i=imax)

create arrayi
Run Code Online (Sandbox Code Playgroud)

其中i是for循环中的变量.

如果imax设置为3,它将产生:array0,array1,array2,array3

谢谢.

Aas*_*set 7

是; 你可以创建一个数组数组.假设您需要以下数组int:

int numberOfArrays = 10;
int[][] arrays = new int[numberOfArrays][];
for (int i = 0; i < numberOfArrays; i++)
    arrays[i] = new int[x]; // Where x is the size you want array i to be
Run Code Online (Sandbox Code Playgroud)

但是,您无法动态创建名为array0,等的变量array1.对于多维数组,就没有必要的变量,比如集合,因为你可以写比较arrays[0],arrays[1]; 这也更灵活,因为你可以索引到数组集合arrays[i],如果你有array0,你就不能这样做array1,等等.