java 初始化 ArrayList<ArrayList<Integer>>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27112973/
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-11-02 11:15:36  来源:igfitidea点击:

Initialize ArrayList<ArrayList<Integer>>

javaarraysarraylist

提问by swing

I met a problem as follow:

我遇到了如下问题:

When I initialize a ArrayList<ArrayList<Integer>>, the codes are:

当我初始化 a 时ArrayList<ArrayList<Integer>>,代码是:

ArrayList<ArrayList<Integer>> group = new ArrayList<ArrayList<Integer>>();
group.add((ArrayList<Integer>) Arrays.asList(1, 2, 3));
group.add((ArrayList<Integer>) Arrays.asList(4, 5, 6));
group.add((ArrayList<Integer>) Arrays.asList(7, 8, 9));
for (ArrayList<Integer> list : group) {
    for (Integer i : list) {
        System.out.print(i+" ");
    }
    System.out.println();
}

Although the codes can be compiled successfully, I still get a exception on console:

虽然可以成功编译代码,但在控制台上仍然出现异常:

Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList at Solution.main(Solution.java:49)

线程“main”中的异常 java.lang.ClassCastException:java.util.Arrays$ArrayList 无法在 Solution.main(Solution.java:49) 处强制转换为 java.util.ArrayList

Thanks for help !

感谢帮助 !

回答by Jon Skeet

Arrays.asListdoesn't return a java.util.ArrayList. It doesreturn an instance of a class called ArrayList, coincidentally - but that's not java.util.ArrayList.

Arrays.asList不返回java.util.ArrayList. 巧合的是,它确实返回了一个名为 的类的实例ArrayList- 但事实并非如此java.util.ArrayList

Unless you needthis to really be an ArrayList<ArrayList<Integer>>I'd just change it to:

除非你真的需要这个,否则ArrayList<ArrayList<Integer>>我会把它改成:

List<List<Integer>> group = new ArrayList<>();
group.add(Arrays.asList(1, 2, 3));
group.add(Arrays.asList(4, 5, 6));
group.add(Arrays.asList(7, 8, 9));
for (List<Integer> list : group) {
    ...
}

If you doneed an ArrayList<ArrayList<...>>- or if you need to be able to add to the "inner" lists even if you don't need them with a static type of ArrayList- then you'll need to create a new ArrayListfor each list:

如果您确实需要ArrayList<ArrayList<...>>- 或者如果您需要能够添加到“内部”列表,即使您不需要它们的静态类型ArrayList- 那么您需要ArrayList为每个列表创建一个新列表:

group.add(new ArrayList<Integer>(Arrays.asList(1, 2, 3)));
// etc

回答by rgettman

The return of Arrays.asListis not a java.util.ArrayList; the java.util.Arrays$ArrayListis a separate class, nested in Arrays, even if it's a List.

的返回Arrays.asList不是java.util.ArrayList; thejava.util.Arrays$ArrayList是一个单独的类,嵌套在 中Arrays,即使它是一个List.

If you must have an ArrayList, then create another ArrayListyourself using the returned Listfrom Arrays.asList, instead of casting, e.g.:

如果您必须有一个ArrayList,则ArrayList使用返回的ListfromArrays.asList来创建另一个自己,而不是强制转换,例如:

group.add(new ArrayList<Integer>( Arrays.asList(1, 2, 3) ));

回答by M Anouti

Arrays.asListreturns an instance of a nested static type java.util.Arrays.ArrayListthat is different from java.util.ArrayList. You can avoid this problem by programming to the Listinterface (which java.util.Arrays.ArrayListimplements as well) and without the unneeded casts:

Arrays.asList返回一个java.util.Arrays.ArrayList不同于的嵌套静态类型的实例java.util.ArrayList。您可以通过对List接口(也java.util.Arrays.ArrayList实现)进行编程来避免此问题,并且无需进行不需要的强制转换:

List<List<Integer>> group = new ArrayList<List<Integer>>();
group.add(Arrays.asList(1, 2, 3));
group.add(Arrays.asList(4, 5, 6));
group.add(Arrays.asList(7, 8, 9));
for (List<Integer> list : group) {
    for (Integer i : list) {
        System.out.print(i+" ");
    }
    System.out.println();
}

回答by Chandrasheker Shivvolla

group.add(new ArrayList<Integer>((Arrays.asList(1, 2, 3))));
group.add(new ArrayList<Integer>((Arrays.asList(4, 5, 6))));
group.add(new ArrayList<Integer>((Arrays.asList(7, 8, 9))));

for (ArrayList<Integer> arrayList : group) {
  for (Integer integer : arrayList) {
    System.out.println(integer);
  }
}

Please refer this code. This might help you out to achieve your requirement.

请参考此代码。这可能会帮助您实现您的要求。