damned
damned

Reputation: 945

ArrayList of Array of Strings

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;

Upvotes: 4

Views: 6929

Answers (6)

Kumar Shorav
Kumar Shorav

Reputation: 531

Yes, first is the Array of ArrayList and will have strings value in it. second statement is only array list of Strings value.

Upvotes: 0

brimborium
brimborium

Reputation: 9522

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[]>();

Upvotes: 3

Crazenezz
Crazenezz

Reputation: 3456

I provide some example to differentiate the Array of ArrayList and ArrayList of String

public class ArrayOfArrayList {

    public static void main(String[] args) {
        // Declare the Array of ArrayList
        List<String>[] arrayOfList = new ArrayList[2];

        // Declare the Object of ArrayList
        for(int i = 0; i < arrayOfList.length; i++) {
            arrayOfList[i] = new ArrayList<>();
            arrayOfList[i].add("" + (i + 1));
            arrayOfList[i].add("" + (i + 2));
        }

        // Print out the result
        for(List<String> list : arrayOfList) {
            for(String str : list) {
                System.out.println(str);
            }
        }

        // Declare the Object of ArrayList
        List<String> arrayList = new ArrayList<>();
        arrayList.add("1");
        arrayList.add("2");

        // Print out the result
        for(String str : arrayList) {
            System.out.println(str);
        }
    }
}

Upvotes: 1

Greg Kopff
Greg Kopff

Reputation: 16625

The second one is an ArrayList, whose elements have type 'String'. But what is the first data structure?

On the surface, it would appear to be an array of lists (containing strings). However arrays and generics don't play very well together. From the article:

Another consequence of the fact that arrays are covariant but generics are not is that you cannot instantiate an array of a generic type (new List<String>[3] is illegal), unless the type argument is an unbounded wildcard (new List<?>[3] is legal). Let's see what would happen if you were allowed to declare arrays of generic types:

List<String>[] lsa = new List<String>[10]; // illegal
Object[] oa = lsa;  // OK because List<String> is a subtype of Object
List<Integer> li = new ArrayList<Integer>();
li.add(new Integer(3));
oa[0] = li; 
String s = lsa[0].get(0); 

The last line will throw a ClassCastException, because you've managed to cram a List<Integer> into what should have been a List<String>. Because array covariance would have allowed you to subvert the type safety of generics, instantiating arrays of generic types (except for types whose type arguments are unbounded wildcards, like List<?>) has been disallowed.

Upvotes: 0

Michael Berry
Michael Berry

Reputation: 72379

The first is an array of classes of the type ArrayList<String>. The second is simply an ArrayList<String> (ArrayList of Strings.)

In terms of initialisations:

ArrayList<String>[] lists = (ArrayList<String>[])new ArrayList[10];
ArrayList<String> temp2 = new ArrayList<String>();

The first initialisation has to specify a size for the array (note this is not a size for the ArrayList) and this is where the 10 comes from in my example. It can be any size you choose of course, 10 is just an arbitrary example. It will also generate a warning, but, if you really want an array of ArrayList<String> this is AFAIK the only way for now (the reason stems from the fact generics in Java aren't reified, but array types are.)

Upvotes: 0

Icarus
Icarus

Reputation: 63970

The first data structure is an array of ArrayLists containing string objects

Upvotes: 0

Related Questions