简体   繁体   English

Java ArrayList of Integer [],如何访问元素

[英]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.

相关问题 如何在Java中迭代整数arraylist的元素 - How to iterate elements of an integer arraylist in Java 反转ArrayList中的元素 <Integer> 爪哇 - Invert Elements in ArrayList<Integer> Java 如何在Java中将键添加为Integer并且值为arraylist的HashMap中添加元素? - how to add elements to HashMap whose key is Integer and value is arraylist in Java? 如何在 ArrayList 中交换元素<arraylist<integer> &gt;? </arraylist<integer> - How to swap elements in ArrayList<ArrayList<Integer>>? 在 Java 中,如何访问整数数组的 ArrayList - In Java, how can I access an ArrayList of integer arrays 如何添加一个ArrayList <Integer> 到ArrayList <ArrayList<Integer> &gt;一次又一次地使用ArrayList中的不同元素 <Integer> - How to add an ArrayList<Integer> to an ArrayList<ArrayList<Integer>> again and again with different elements in ArrayList<Integer> Java添加ArrayList <Integer> 到ArrayList <ArrayList<Integer> &gt;替换ArrayLists的ArrayList的所有元素 - Java adding ArrayList<Integer> to ArrayList<ArrayList<Integer>> replaces all elements of the ArrayList of ArrayLists 如何访问ArrayList中的ArrayList? Java的 - How to access an ArrayList in an ArrayList? Java JAVA:如何将String ArrayList转换为Integer Arraylist? - JAVA: How to convert String ArrayList to Integer Arraylist? 如何访问(修改、添加元素)到 Java 内部类中的 ArrayList/Collection? - How to access (modify, add elements) to an ArrayList/Collection in an Inner class in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM