user2458768
user2458768

Reputation: 115

set value in 2d arraylist Java

i have 2d ArrayList:

ArrayList<List<Integer>> group;
group.add(Arrays.asList(i1, i2, i3));
group.add(Arrays.asList(i4, i5, i6));
group.add(Arrays.asList(i7, i8, i9));

how to set value on for example i5?

i should use:

group.set(index, value); 

but how get correct index i5?

Upvotes: 3

Views: 27557

Answers (1)

Alexis C.
Alexis C.

Reputation: 93842

You should get the second List first and then set the element in this list.

So it should be :

group.get(1).set(1, value); 
       ^      ^
       |      |
       |     set the second value of this list to value
       |
get the second List

Demo here.

If you want to write a method to set the value of the element you want you can do (you may check for indexes) :

public static void setValue(List<List<Integer>> list, int row, int column, int value){
     list.get(row).set(column, value);
}

Upvotes: 16

Related Questions