[英]Java ArrayList of Integer[], how to access elements
I am using an ArrayList of to create lists. 我正在使用ArrayList创建列表。 For example:
例如:
Index 0: 1 3
Index 1: 4 5
Index 2: 1 3 7
How can I access the second element of the first index of the ArrayList? 如何访问ArrayList的第一个索引的第二个元素? Couldn't find the answer on Google, so I asked here.
在Google上找不到答案,所以我在这里问。
yourList.get(0)[1]; // that's it !!
If you want to iterate over it : 如果要遍历它:
for (Integer[] outer : yourList) {
for(Integer inner : outer) {
System.out.println(inner);
}
}
By your question, I am guessing you have something like this? 根据您的问题,我猜您有这样的事情吗?
List<Integer[]> list = new ArrayList<Integer[]>();
Integer[] a1 = {1,3};
Integer[] a2 = {4,5};
Integer[] a3 = {1,3,7};
list.add(a1);
list.add(a2);
list.add(a3);
Then all you need to do is simply call: 然后,您只需要简单地调用:
Integer result = list.get(0)[1];
The get(0)
pulls the first Integer[]
out of the list, then to get the second element, you use [1]
get(0)
将第一个Integer[]
从列表中拉出,然后使用[1]
来获取第二个元素
where do you see the exception? 您在哪里看到例外情况? have you tried this?
你尝试过这个吗?
List<Integer[]> list = new ArrayList<Integer[]>(3);
Integer[] a1 = {1,3};
Integer[] a2 = {4,5};
Integer[] a3 = {1,3,7};
list.add(a1);
list.add(a2);
list.add(a3);
Integer result = list.get(0)[1];
There aren't any exception. 也不例外。 The arraylist have three elementes because you have three elemente (a1,a2,a3),
arraylist具有三个元素,因为您具有三个元素(a1,a2,a3),
You have List<Integer[]> l = new ArrayList<Integer[]>(3);
您具有
List<Integer[]> l = new ArrayList<Integer[]>(3);
if you want the second element of the first index: 如果要第一个索引的第二个元素:
l.get(0)[1].
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.