set value in 2d arraylist Java

17,336

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);
}
Share:
17,336
user2458768
Author by

user2458768

Updated on June 27, 2022

Comments

  • user2458768
    user2458768 almost 2 years

    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?