Java 如何从二维数组列表中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26154120/
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
how to get value from 2d arraylist
提问by Piumi Wandana
i have arraylists named sub and main,
我有名为 sub 和 main 的数组列表,
ArrayList main = new ArrayList();
ArrayList sub=new ArrayList();
i add value to sub and then add sub to main.
我向 sub 添加值,然后将 sub 添加到 main。
example;
例子;
sub.add(1);
sub.add(2);
main.add(sub);
now i want to get all values inside sub
现在我想获取 sub 中的所有值
so i used following one but .get(j) gives me the error get >> canot find symbol
for (int i=0;i<main.size();i++) {
System.out.println();
for (int j=0;j<sub().size();j++) {
System.out.print(main.get(i).get(j));//error line
}
}
how can i get all values inside subarray of main arraylist
如何获取主数组列表子数组中的所有值
采纳答案by Code-Apprentice
When you declare a variable as
当您将变量声明为
ArrayList main;
This list holds Object
s. This means that main.get(i)
will only return an Object
, even if you add ArrayList
s. That's why you get a compiler error: Object
doesn't have a method named get()
.
该列表包含Object
s。这意味着即使添加smain.get(i)
也只会返回 an 。这就是您收到编译器错误的原因:没有名为.Object
ArrayList
Object
get()
To fix the problem, you need to use generics:
要解决此问题,您需要使用泛型:
ArrayList<List<Integer>> main = new ArrayList<>();
ArrayList<Integer> sub=new ArrayList<>();
Now get()
will return a List<Integer>
which has a get()
method, so the compiler error will disappear.
现在get()
将返回List<Integer>
具有get()
方法的a ,因此编译器错误将消失。
回答by misberner
Generics could be your friend here:
泛型在这里可能是你的朋友:
ArrayList<ArrayList<Object>> main = new ArrayList<ArrayList<Object>>(); // or new ArrayList<>(); in Java 7+
ArrayList<Object> sub = new ArrayList<Object>(); // or new ArrayList<>();
If you can't or don't want to use generics, the solution is to cast the expression main.get(i)
to an ArrayList
first:
如果您不能或不想使用泛型,解决方案是将表达式main.get(i)
转换为ArrayList
第一个:
System.out.println(((ArrayList) main.get(i)).get(j));
回答by Dhanushka Gayashan
Go through the following code
通过以下代码
public class ArrayListDemo {
public static void main(String[] args) {
List<List<Integer>> main = new ArrayList<>();
List<Integer> sub = new ArrayList<>();
sub.add(1);
sub.add(2);
main.add(sub);
//If you want to get values in sub array list
for(int i = 0; i < 1; i++){
List<Integer> arr = main.get(i);
for(Integer val : arr) System.out.println(val + "");
}
//If you want to print all values
for(List<Integer> list : main){
for(Integer val : list) System.out.println(val + "");
}
}
}
}
In the above code, I had declared an ArrayList (main) to keep all Array which are having Integer values. Also i had declared an another ArrayList (sub) to keep all Integer values. I had used ArrayList data structure because of length of the List will be changing the run time.
在上面的代码中,我声明了一个 ArrayList (main) 来保留所有具有 Integer 值的 Array。我还声明了另一个 ArrayList (sub) 来保留所有 Integer 值。我曾经使用过 ArrayList 数据结构,因为 List 的长度会改变运行时间。
Good Luck !!!
祝你好运 !!!