I am reading Arrays in Java. I want to dynamically create a 2D array in java. I know I've to use Arraylist but I don't know how to actually write the elements to location.
I come from C language background. It is quite easy to create a dynamic array and add elements to it using for loop. However, the same logic didn't work here. I've read another answers but they are not helpful, they're using either using advanced concepts or using static declaration.
ArrayList<Integer> Arr1=new ArrayList<>();
ArrayList<ArrayList<Integer>> Arr2=new ArrayList<ArrayList<Integer>>();
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
Arr1.add(j);
}
Arr2.add(Arr1);
}
My implementation is wrong but that is the closest I can think about writing elements to a dynamic 2D array in java.
Can someone please help me in understanding how to write elements to a specific row and to a specific column