ArrayList of Array of Strings

arraylist, arrays, java, string

Solution

`ArrayList<String>[] temp1;`: This is an Array of ArrayList's that are containing Strings

`ArrayList<String> temp2;`: This is an ArrayList containing Strings

If you want an ArrayList of Arrays of Strings, you would have to do a `ArrayList<String[]> temp3;`. Note the position of the different brackets.

To initialize:

// create an array with 10 uninitialized ArrayList<String>
ArrayList<String>[] temp1 = new ArrayList[10];
// create empty lists that can be filled
for (int i=0; i<temp1.length; i++)
  temp1[i] = new ArrayList<String>();

// create an empty list of Strings
ArrayList<String> temp2 = new ArrayList<String>();

// create an empty list of String arrays
ArrayList<String[]> temp3 = new ArrayList<String[]>();

Problem

What is the difference between the two data structures defined below? The second one is an ArrayList, whose elements have type 'String'. But what is the first data structure? The initializations would also be different. Can anyone give an example here? ``` ArrayList<String>[] temp1; ArrayList<String> temp2; ```

Original source