简体   繁体   中英

how to create multi dimensional array dynamic in java?

I have my array which is static, now i want to make it dynamic using for loop

int[][] args = new int[][]{{6815, 11524},{6845, 11567},{6815, 11524}};

I want add value using for loop, how can i do it

I tried but don't get complete, like

for(int k=0;k<5;k++){
  // here i can add both both value {6815, 11524} as int.
}

Is it possible to do it ?

I believe that what you want to do is this:

// Create a 2D array of length of 5
int[][] args = new int[5][];
// Iterate from 0 to 4
for(int k=0;k<5;k++){
    // Affect the value of the array for the index k
    args[k] = new int[]{6815, 11524};
}

NB: If you don't know the size of your array, consider using a List instead. The code will then be something like that:

List<int[]> args = new ArrayList<>();
...
args.add(new int[]{6815, 11524});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM