我想创建一个包含5个索引的数组,每个索引都有一个索引,每个索引包含另一个整数 . 5索引中的每个数组都需要和以下一样多的索引(10,100,1000,10000),但我不知道如何在我的for循环中填充这样的数组,这通常与数组一起使用,没有它运行到无穷大,因为我有(int x = 0; x <array.length; x),我不能在这里使用x变量; int [x < - (syntax error)] array = {ten = new int [arraySize],hundred = new int [arraySize],thousand = new int [arraySize],tenthousand = new int [arraySize],没有它告诉我有一个语法错误 . 我不知道该怎么办 .
所有这些代码都是它自己的类的方法的一部分,如果这有助于更好地理解 .
public int ArrayArray(int arraySize,int randomNumber){
arraySizes = arraySize;
for(int x = 0; x < array.length; x++) {
size = 0;
Random gen = new Random(randomNumber);
int[]ten;
ten = new int[arraySize];
int[] hundred;
hundred = new int[arraySize];
int[]thousand;
thousand = new int[arraySize];
int[]tenThousand;
tenThousand = new int[arraySize];
int[] array = {ten[x] = gen.nextInt(10), hundred[x] = gen.nextInt(100),
thousand[x] = gen.nextInt(1000), tenThousand[x] = gen.nextInt(10000)};
return array[];
}
这改变了我的问题,我认为在完成它之后我已经得到了它 . 这看起来会回归我想要的吗?我将有一个驱动程序,我将使用给定的数组大小和给定数量的随机整数调用此方法 .
2 回答
如果您尝试返回并对函数进行数组运算,请尝试以下方法:
注意它是
int[][]
而不是int[]
. 这是因为您尝试返回的二维数组不仅仅是一维数组 .让我知道这是如何工作的 .
如果您正在尝试创建数组数组,那么多维数组就是最佳选择 .
绕过二维数组也与数组相同 . 例如 .