ArrayList<ArrayList<String>, ArrayList<String>, ArrayList<String>> newlist = new ArrayList<ArrayList<String>, ArrayList<String>, ArrayList<String>>();
Above is my horrible ArrayList
definition. I am trying to define an ArrayList
containing multiple ArrayList
inside although it gives me an Incorrect number of arguments
error.
facilities = {
[parking]
[bike]
[disability]
}
I am trying to make an ArrayList
to hold this data above The main ArrayList
(Facilities) will contain the inner ArrayLists
. What is the correct way to define an ArrayList
of ArrayList
s?
First, ArrayList
has only one type parameter, the type of all objects it contains, not each individual object. Second, an ArrayList
is not the correct choice for storing related data that should be in its own object.
Fixing just the first problem would result in you defining your ArrayList
as:
ArrayList<ArrayList<String>>
Additionally, usually you would program to the interface and define the variable as
List<List<String>>
You are free to use ArrayList
or any other list as concrete implementations, of course, eg:
List<List<String>> newList = new ArrayList<>();
The second thing to fix would be to create a Facility
class to encapsulate parking, bike, and disability. Then your list to hold Facility
objects becomes simply:
List<Facility> newList = new ArrayList<>();
You use:
ArrayList<ArrayList<String>> array = new ArrayList<>();
array.add(//Your arrayList here);
You add ArrayList
s to it just like you would String
s to a regular ArrayList
of String
s.
ArrayList<ArrayList<String>> listOfLists = new ArrayList<>();
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.