Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 1.6: Creating an array of List<T>

Tags:

java

generics

Why can't I create an array of List ?

List<String>[] nav = new List<String>[] { new ArrayList<String>() }; 

Eclipse says "Cannot create a generic array of List"

or

ArrayList<String>[] nav = new ArrayList<String>[] { new ArrayList<String>() }; 

Eclipse says "Cannot create a generic array of ArrayList"

or

List<String>[] getListsOfStrings() {     List<String> groupA = new ArrayList<String>();     List<String> groupB = new ArrayList<String>();     return new List<String>[] { groupA, groupB }; } 

But I can do this:

List[] getLists() {     return new List[] { new ArrayList(), new ArrayList() }; } 

Eclipse says that List and ArrayList are raw types but it compiles...

Seems pretty simple, why won't it work?

like image 749
Mike Avatar asked Apr 14 '11 11:04

Mike


People also ask

Can we create list of list in Java?

You can declare a List of Lists in Java, using the following syntax. It uses the new operator to instantiate the list by allocating memory and returning a reference to that memory. To add elements to it, call the add() method.

Can you turn a list into an array Java?

The best and easiest way to convert a List into an Array in Java is to use the . toArray() method. Likewise, we can convert back a List to Array using the Arrays. asList() method.

Can you make an array of list?

If you are not sure about the type of objects in the array or you want to create an ArrayList of arrays that can hold multiple types, then you can create an ArrayList of an object array. Below is a simple example showing how to create ArrayList of object arrays in java.


2 Answers

Well, generics tutorial give the answer to your question.

The component type of an array object may not be a type variable or a parameterized type, unless it is an (unbounded) wildcard type.You can declare array types whose element type is a type variable or a parameterized type, but not array objects.

This is annoying, to be sure. This restriction is necessary to avoid situations like:

// Not really allowed. List<String>[] lsa = new List<String>[10]; Object o = lsa; Object[] oa = (Object[]) o; List<Integer> li = new ArrayList<Integer>(); li.add(new Integer(3)); // Unsound, but passes run time store check oa[1] = li;  // Run-time error: ClassCastException. String s = lsa[1].get(0); 

If arrays of parameterized type were allowed, the previous example would compile without any unchecked warnings, and yet fail at run-time. We've had type-safety as a primary design goal of generics.

like image 94
Gursel Koca Avatar answered Sep 26 '22 22:09

Gursel Koca


You can't create arrays of generic types, generally.

The reason is that the JVM has no way to check that only the right objects are put into it (with ArrayStoreExceptions), since the difference between List<String> and List<Integer> are nonexistent at runtime.

Of course, you can trick the compiler by using the raw type List or the unbound wildcard type List<?>, and then cast it (with a unchecked cast) to List<String>. But then it is your responsibility to put only List<String> in it and no other lists.

like image 35
Paŭlo Ebermann Avatar answered Sep 22 '22 22:09

Paŭlo Ebermann