Array_2D solution

programming
Tags: #<Tag:0x00007f2423f086d0>

#1

In our performOps() method below is the functionality.

for (int i = 0; i < A.size(); i++) {
            B.add(new ArrayList<Integer>());
        
            for (int j = 0; j < A.get(i).size(); j++) {   **---------------------> 1st inner loop**
                B.get(i).add(0);   ..**................................**1 line**
            }

            for (int j = 0; j < A.get(i).size(); j++) {  **------------------------> 2nd inner loop**
                B.get(i).set(A.get(i).size() - 1 - j, A.get(i).get(j)); **------------------ 2line**
            }
        }

The line will make all the B ArrayList to zero(0). So the end of the 1st inner loop, we have  all value as 0 like B=[[0,0,0,0],[0,0,0,0],[0,0,0,0]]
Then when control comes to 2nd inner loop, it starts assign the value from A list to B by reverse order of each inner list from A.
So at the end we will have [[4,3,2,1],[8,7,6,5],[12,11,10,9]]

And when this looping will happen at last
for (int i = 0; i < B.size(); i++) {
    for (int j = 0; j < B.get(i).size(); j++) {
            System.out.print(B.get(i).get(j) + " ");
    }
}

It gives the o/p 4,3,2,1,8,7,6,5,12,11,10,9.

Hopes this helps.