4

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;
1
  • This is a basic issue in java. You have to know it if you programming any simple program. First structure is a array of ArrayList and second is a ArrayList. You could find very web page taht explain [] and Collection APIconcepts. It is very simple. Commented May 15, 2012 at 10:40

6 Answers 6

3

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[]>();
Sign up to request clarification or add additional context in comments.

1 Comment

Type erasure means that your cast to ArrayList<String>[] is reduced to a cast to ArrayList[] - which is unnecessary. Arrays and generics don't work well together.
1

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);
        }
    }
}

Comments

0

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

Comments

0

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.)

1 Comment

@berry120: see here for why this doesn't work: ibm.com/developerworks/java/library/j-jtp01255/index.html
0

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.

2 Comments

The last line will throw a ClassCastException, because you've managed to cram a List Integer into what should have been a List String.
@Will: fixed (SO doesn't like angle brackets in quote blocks it seems).
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.