-1
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 ArrayLists?

Neuron
  • 5,141
  • 5
  • 38
  • 59
unkwndev
  • 99
  • 1
  • 8

3 Answers3

2

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, e.g.:

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<>();
rgettman
  • 176,041
  • 30
  • 275
  • 357
0

You use:

ArrayList<ArrayList<String>> array = new ArrayList<>();
array.add(//Your arrayList here);

You add ArrayLists to it just like you would Strings to a regular ArrayList of Strings.

Neuron
  • 5,141
  • 5
  • 38
  • 59
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
0

ArrayList<ArrayList<String>> listOfLists = new ArrayList<>();

Owen
  • 919
  • 5
  • 11