Ian
Ian

Reputation: 561

Compilation error: Generic array creation

I would like to create an array of ArrayList<String>. I tried the following:

static ArrayList<String>[] displayBlocks = new ArrayList<String>[3];

However, I'm getting a compile time error:

generic array creation

I have added import java.util.*;. How can I get it to compile?

Upvotes: 3

Views: 7431

Answers (5)

Ted Hopp
Ted Hopp

Reputation: 234857

From the Java Language Specification section on array creation expressions:

It is a compile-time error if the ClassOrInterfaceType does not denote a reifiable type (§4.7). . . . The rules above imply that the element type in an array creation expression cannot be a parameterized type, other than an unbounded wildcard.

Like others have said, use an ArrayList of ArrayLists, or else use a cast (where you might want to also suppress unchecked conversion warnings).

Upvotes: 0

BalusC
BalusC

Reputation: 1109874

This construct is indeed not allowed in Java. You could use the varargs hack:

static List<String>[] displayBlocks = createArray(3);

with

public static <E> E[] createArray(int length, E... elements) {
    return Arrays.copyOf(elements, length);
}

Needless to say, a List<List<String>> is better, unless you're extremely tight on memory, but then I'd wonder why you don't use a String[][].

Upvotes: 5

DarthVader
DarthVader

Reputation: 55132

if you want an array of arraylist:

import java.util.ArrayList;
import java.util.List;

public class Foo{

    List [] arrayOfLists = new ArrayList[10];


}

Here is a related post. you cant create a generic array of arraylist. You can do this:

import java.util.ArrayList;
import java.util.List;

public class Foo{

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


}

Upvotes: 11

Marcus
Marcus

Reputation: 12596

A side from the other answer one can also create the list like this, which might feel a little bit more familiar

static ArrayList<String>[] displayBlocks = (ArrayList<String>[]) new ArrayList[3];

Upvotes: 2

jayunit100
jayunit100

Reputation: 17650

What you are doing wrong :

You have not created an object. Remember that in java

1) All objects have constructors. 2) Constructors are methods. 3) To create an object, you must call the constructor , thus, you are calling a method !

Your desire is ambiguous . But it will be good to read both of these answers :

1) You could make an Array of array lists : This is a data structure with 10 array lists. it is essentially a 2D array where the amount of columns per row is variable, but there will only be 10 rows.

This would be an odd data structure to create. It might represent, for example, a domain model where you have exactly 10 people and each of those people had a variable number of dvds in their dvd collection. so you would have 10 rows, each one with an array list in it.

static ArrayList[] displayBlocks = new ArrayList[10];

  • not that there is no () here, just a [] * This is because we havent populated our array, rather, we just declare that there is an array with 10 slots, and each slot will have type "ArrayList". To populate, you would have to iterate and add displayBlock[0]=new ArrayList(), for 0-9.

2) Probably, you just wanted a simple array list , which is a linear collection of items.

To do this you would simply declare :

Collection a = new ArrayList(); //or List a = new ArrayList();

Now ... you should ask yourself why you can declare the variable as either a Collection or a List, if you want to really java's collections work.

Upvotes: -1

Related Questions