How to initialize an array in Java?

I use the size of the array in the variables in the loop Every time I have to specify that the size of the array is equal to the variable, and then take an integer equal to the size For example:

for(i = 0; i < N; i++)
{
    variable = sc.nextInt();
    int []array = new int[variable];
    for(j = 0; j < variable; j++)
    {
        array[j] = sc.nextInt();
    }
}

Please give me the most effective method, because I am a novice in Java:)

Solution

Maybe you need something like this:

List<int[]> list = new ArrayList<>();//create a list or arrays
for (int i = 0; i < n; i++) {

    int variable = sc.nextInt();
    int[] array = new int[variable];
    for (int j = 0; j < variable; j++) {
        array[j] = sc.nextInt();
    }

    list.add(array);//add your array to your list
}
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
分享
二维码
< <上一篇
下一篇>>