Java – Compilation error: Generic array creation

java

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?

Best Answer

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


}