3

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?

1 Answer 1

16

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.