The essence of the task is to create an ArrayList and add ordinary arrays to it, which must then be filled with data. There was such a question, how are arrays filled in an ArrayList? Specifically incomprehensible entry in the internal cycle:

for (int i = 0; i < nums.size(); i++) { for (int j = 0; j<nums.get(i).length; j++ ) { nums.get(i)[j] = i; } } 

Here is the whole code of the method:

 public static ArrayList<int[]> createList() { ArrayList<int[]> nums = new ArrayList<int[]>(); nums.add(new int[5]); nums.add(new int[2]); nums.add(new int[4]); nums.add(new int[7]); nums.add(new int[0]); Random r = new Random(); for (int i = 0; i < nums.size(); i++) { for (int j = 0; j<nums.get(i).length; j++ ) { nums.get(i)[j] = i; } } return nums; } 

    1 answer 1

    The get(i) method of the nums object of type ArrayList<int[]> returns the i th element of the nums list, which is an object of type int[] - an array of integers. Further, in the internal loop, this array is initialized with values.

    • nums.get(i)[j] = i; it turns out in this record get(i) gets int[] from ArrayList, and by the value of [j] resulting array is filled with data (assigned a value), I understand it correctly? - Alexey Bogdan
    • The get(i) method does not receive , but returns int[] (it receives i ). The index j indicates to which element of the resulting array int[] to assign the value i . - post_zeew