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
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
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
. If you do you will be writing your own implementation of ArrayList
on the lines below it. In stead you just want to use the standard ArrayList
so terminate that line and start a new one.
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
. In fact what Arrays.asList
creates is not an ArrayList
so it wouldn't fit otherwise.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.