首页 文章

我需要知道如何用其他数组填充数组索引

提问于
浏览
0

我想创建一个包含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 回答

  • 0

    如果您尝试返回并对函数进行数组运算,请尝试以下方法:

    public int[][] ArrayArray(int arraySize, int randomNumber) {
        Random gen = new Random(randomNumber);
         int[]ten= new int[10];
         int[] hundred= new int[100];
         int[]thousand= new int[1000];
         int[]tenThousand= new int[10000];     
         int[][] array = {ten,hundred,thousand,tenthousand};
        return array;
    }
    

    注意它是 int[][] 而不是 int[] . 这是因为您尝试返回的二维数组不仅仅是一维数组 .

    让我知道这是如何工作的 .

  • 0

    如果您正在尝试创建数组数组,那么多维数组就是最佳选择 .

    int arraySize = 10;
    
    int[]ten = new int[arraySize];
    int[] hundred = new int[arraySize];
    int[]thousand = new int[arraySize];
    int[]tenThousand = new int[arraySize];
    
    int[][] array = new int[4][arraySize];
    
    array[0] = ten;
    array[1] = hundred;
    array[2] = thousand;
    array[3] = tenThousand;
    

    绕过二维数组也与数组相同 . 例如 .

    public static int hello(int[][] pass) {
        return pass;
    }
    

相关问题