geo
geo

Reputation: 527

Retrieve data from List of arrays

I am declaring an list of arrays of integers.

  List<int[]> list = new ArrayList<>();

I populate it:

  for (int i=0; i<10; i++){
    for (int j=1; j<10; j++){ 
      int[] currentadding = {i,j};
      list.add(currentadding);
    }
  }

How do I read the values stored?

Upvotes: 0

Views: 104

Answers (3)

daark
daark

Reputation: 326

you can iterate over array lists with a for loop like

for(int[] x : list){
 // do something to x
}

or if you know the index of what your looking for you can use

list.get(*index*);

Upvotes: 1

harsh
harsh

Reputation: 7692

you can foreach:

for(int[] currentadding : list) {
// access mutate currentadding
}

Upvotes: 1

Rahul
Rahul

Reputation: 45060

You can just iterate over the list using the advanced for-loop and use the int[] you get from the list on every iteration.

for(int[] arr : list) {
    System.out.println(Arrays.toString(arr));
    // System.out.println(arr[0]);
}

Or you can use the standard for-loop as well.

for(int i=0; i < list.size(); i++) {
    int[] arr = list.get(i);
    System.out.println(Arrays.toString(arr));
    // System.out.println(arr[0]);
}

Upvotes: 1

Related Questions