[英]How to declare a multiple arraylist in java
I have a problem to declare a mulitple arraylist.我在声明多个数组列表时遇到问题。 ... missing the correct syntax or mixed up my mind ...
... 缺少正确的语法或混淆了我的想法 ...
This works fine till here, to declare an arraylist with an array:这工作正常,直到这里,用数组声明一个数组列表:
Integer[] arrArray = new Integer[] {1,2,3,4,5};
ArrayList<Integer> arrList = new ArrayList<Integer>();
arrList.addAll(Arrays.asList(arrArray));
also initailising and declaring in one line :也在一行中初始化和声明:
Integer[] arrArray = new Integer[] {1,2,3,4,5};
ArrayList<Integer> arrList = new ArrayList<Integer>(){{
addAll(Arrays.asList(arrArray));}};
or by element :或按元素:
ArrayList<Integer> arrList = new ArrayList<Integer>(){{
add(1);
add(2);
add(3);
}};
But now I would like to do the same thing, but now with multiple dimension arraylists (and Strings in this case):但现在我想做同样的事情,但现在使用多维数组列表(在这种情况下为字符串):
private String[] arrFUSE={"3", "true", "0", "FUSE"};
private String[] arrPLACE={ "2", "true", "7", "PLACE"};
private ArrayList<ArrayList<String>> arrLIST = new ArrayList<ArrayList<String>>(){{
add( Arrays.asList(arrFUSE) );
add( Arrays.asList(arrPLACE) );
}};
An other try另一个尝试
private ArrayList<ArrayList<String>> arrLIST =
new ArrayList<ArrayList<String>>(){{
add(
new ArrayList<String>(){{ addAll(Arrays.asList(arrFUSE))}};
);
}};
Thanks on beforehand.先谢谢了。
Try casting asList
result to ArrayList
since asList
return of type List
尝试将
asList
结果转换为ArrayList
因为asList
返回类型List
ArrayList<ArrayList<String>> arrLIST = new ArrayList<ArrayList<String>>(){{
add( (ArrayList<String>) Arrays.asList(arrFUSE) );
add( (ArrayList<String>) Arrays.asList(arrPLACE) );
}};
or change declaration to List
或将声明更改为
List
ArrayList<List<String>> arrLIST = new ArrayList<List<String>>(){{
add( Arrays.asList(arrFUSE) );
add( Arrays.asList(arrPLACE) );
}};
Since you are implementing anonymous inners class right-away, there is no way to find out it's type which is present in outside of it's scope.由于您正在立即实现匿名内部类,因此无法找出存在于其范围之外的类型。 That's where casting needed.
这就是需要铸造的地方。
String[] arrFUSE={"3", "true", "0", "FUSE"};
String[] arrPLACE={ "2", "true", "7", "PLACE"};
List<List<String>> arrLIST = new ArrayList<List<String>>();
arrLIST.add( Arrays.asList(arrFUSE) );
arrLIST.add( Arrays.asList(arrPLACE) );
Don't use {
on the line you create a new instance of the ArrayList
.不要在创建
ArrayList
的新实例的行上使用{
。 If you do you will be writing your own implementation of ArrayList
on the lines below it.如果您这样做,您将在其下方的行中编写您自己的
ArrayList
实现。 In stead you just want to use the standard ArrayList
so terminate that line and start a new one.相反,您只想使用标准的
ArrayList
因此终止该行并开始一个新的行。
I also changed ArrayList<ArrayList<String>>
to List<List<String>>
because you want to be able to put any List
in there, not just ArrayLists
.我还将
ArrayList<ArrayList<String>>
更改为List<List<String>>
因为您希望能够将任何List
放入其中,而不仅仅是ArrayLists
。 In fact what Arrays.asList
creates is not an ArrayList
so it wouldn't fit otherwise.事实上
Arrays.asList
创建的不是ArrayList
所以它不适合其他地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.