在二维数组列表 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:05:55  来源:igfitidea点击:

set value in 2d arraylist Java

javaarraylistmultidimensional-array

提问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 Listfirst 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

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);
}