Java: how to create a loop to create an array?

I need a lot of arrays, now I create them manually:

int[] row1 = new int[3];
int[] row2 = new int[3];
int[] row3 = new int[3];

I want to create these in the array as follows:

public final int SIZE = 3;
for (int i = 1;i <= 3;i++)  
  int[] row[i] = new int[3];

But I don't know how to generate an array in a loop

What should I do?

Specifically, how to generate dynamic identifiers in each iteration?

Solution

You don't have. Instead, you realize that you actually have a collection of arrays For example, you can:

int[][] rows = new int[3][3];

or

List<int[]> rows = new ArrayList<>();
for (int i = 0; i < 3; i++) {
    rows.add(new int[3]);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>