use*_*768 2 java arraylist multidimensional-array
我有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));
Run Code Online (Sandbox Code Playgroud)
如何设置值例如i5?
我应该使用:
group.set(index, value);
Run Code Online (Sandbox Code Playgroud)
但如何获得正确的索引i5?
您应该先获得第二个List,然后在此列表中设置元素.
所以它应该是:
group.get(1).set(1, value);
^ ^
| |
| set the second value of this list to value
|
get the second List
Run Code Online (Sandbox Code Playgroud)
如果你想编写一个方法来设置你想要的元素的值(你可以检查索引):
public static void setValue(List<List<Integer>> list, int row, int column, int value){
list.get(row).set(column, value);
}
Run Code Online (Sandbox Code Playgroud)