3

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?

7
  • 1
    Did you import java.util.ArrayList? What's the error message? Is the compiler set to treat unchecked conversions as errors? Commented Oct 23, 2011 at 1:33
  • 1
    Where did you put this initialization? Is it inside a class definition, outside all the methods? Commented Oct 23, 2011 at 1:36
  • I think i responded to both your comments in the post. Commented Oct 23, 2011 at 1:39
  • possible duplicate of Java generics and array initialization Commented Oct 23, 2011 at 1:42
  • see also: stackoverflow.com/questions/1493162/… Commented Oct 23, 2011 at 1:43

5 Answers 5

11

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


}
Sign up to request clarification or add additional context in comments.

4 Comments

No, i want an array of arraylists. Thanks for helping.
Thanks, It did compile, but can you explain a little more of what's going on there?
Based on the post i referenced, "You can't use generic array creation. It's a flaw/ feature of java generics. "
if you really want to used fixed sized arrays to get some performance boost you might use an array of Object and store the arrayList object references inside the array of objects. Although you will need to cast the references back to use the array Lists
5

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[][].

1 Comment

It's not a hack, it's 100% sound and legit, it'll work even if Java adds full reification in future. I'll claim credit for this trick. stackoverflow.com/questions/7857282/…
2

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];

Comments

0

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

Comments

-1

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.

1 Comment

Thanks for the response, I do want to create an array of arraylists.

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.