3

I am trying to work with arrayList as it is asked in many codding competitions. I want to get familiarized with the arraylist as i am with normal int arrays. It requires 2 different arrayLists and then first we have add the element in one arraylist which is for row elements and another is for column elements.

List<List<Integer>> arr = new ArrayList<List<Integer>>();
List<Integer> arrCol = new ArrayList<Integer>();
Scanner scn = new Scanner(System.in);
for (int i = 0; i < arr.size(); i++) {
    for(int j = 0; j < arrCol.size(); j++) {
        int x = scn.nextInt();
        arrCol.add(j, x);
    }
    arr.add(i, arrCol);
}
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
Naman Chauhan
  • 41
  • 1
  • 1
  • 5

2 Answers2

7

I think what you're asking is how to do this:

List<List<Int>> arrayList = new ArrayList(); //Java usually infers type parameters in cases as these
for(int i = 0; i < desiredSize; i++){
    List<Int> listAtI = new ArrayList ();
    for(int j = 0; j < rowLength; j++){
        listAtI.set(j, 0);  //sets the element at j to be  0, notice the values are Int not int, this is dues to Javas generics having to work with classes not simple types, the values are (mostly) automatically boxed/unboxed
    }
    arrayList.set(i, listAtI);
}

arrayList.get(5); //returns the list at index 5
arrayList.get(5).get(5) // returns values from column 5 in row 5 

If you're unfamiliar with lists in general, reading the answer here should provide valuable information on when to use which type of list

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
rasmus91
  • 3,024
  • 3
  • 20
  • 32
  • What is this 'Int' type please? – hjr2000 May 10 '20 at 12:48
  • @hjr2000 it's a boxed version of `int` as the common integer in Java is not an 'object' and thus can be used with generics. Boxing and unboxing happens automatically, so there is no need to bother with explicit casting, etc. – rasmus91 May 10 '20 at 17:36
  • 1
    Rasmus thanks. It's a bit confusing as obviously there is no Int wrapper class in Java and the Int class isn't defined in the snippet. – hjr2000 May 10 '20 at 23:34
6

You can do this operation similar as we do with the two-dimensional arrays using two for-loops:

int rowSize = 5;
int colSize = 3;
List<List<Integer>> arr = new ArrayList<List<Integer>>();
for (int i = 0; i < rowSize; i++) {
    List<Integer> arrRow = new ArrayList<Integer>();
    for (int j = 0; j < colSize; j++) {
        int x = scn.nextInt();
        arrRow.add(x);
    }
    arr.add(arrRow);
}

You can relate above code to this one:

int rowSize = 5;
int colSize = 3;
int[][] arr = new int[rowSize][colSize];
for (int i = 0; i < rowSize; i++) {
    for (int j = 0; j < colSize; j++) {
        int x = scn.nextInt();
        arr[i][j] = x;
    } 
}

And fetching data from that list is more simple. For the second code above (using arrays), we can print all the values of the two-dimensional array using:

for (int i = 0; i < rowSize; i++) {
    for (int j = 0; j < colSize; j++) {
        System.out.print(arr[i][j] + " ");
    }
    System.out.println();
}

And in case of arraylist, similar thing can be done as:

for (int i = 0; i < rowSize; i++) {
    for (int j = 0; j < colSize; j++) {
        System.out.print(arr.get(i).get(j) + " ");
    }
    System.out.println();
}
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
  • Where scn.nextInt(); is basically the value that you would like to populate that particular element with. – hjr2000 May 10 '20 at 13:05
  • @hjr2000 Yes, you are right. In this case, OP is taking input from console, which has been retrieved using scn.nextInt(); – UkFLSUI May 11 '20 at 04:31