在二维数组列表 Java 中设置值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19893275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set value in 2d arraylist Java
提问by user2458768
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?
如何在例如 i5 上设置值?
i should use:
我应该使用:
group.set(index, value);
but how get correct index i5?
但是如何获得正确的索引 i5?
采纳答案by Alexis C.
You should get the second List
first and then set the element in this list.
您应该先获取第二个List
,然后在此列表中设置元素。
So it should be :
所以应该是:
group.get(1).set(1, value);
^ ^
| |
| set the second value of this list to value
|
get the second List
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);
}