-6
public class XXXX{
    private List<Integer>[] a;
    public XXXX(int num)
    {
        a = new ....?
    } 
}

How should i new the a ?

Eric
  • 87,154
  • 48
  • 211
  • 332
victor_zhang
  • 27
  • 1
  • 1
  • 7
    Arrays and generics don't go well together. – NPE Mar 26 '13 at 12:00
  • what is a? is it a array of List – Arun P Johny Mar 26 '13 at 12:00
  • Why would you even need such a construct? (apart from an interview or exam question...) – ppeterka Mar 26 '13 at 12:03
  • You cannot create an array of a parameterised type in Java. See: http://stackoverflow.com/questions/3903196/error-generic-array-creation – Jesper Mar 26 '13 at 12:08
  • possible duplicate of [Initialize Java Generic Array of Type Generic](http://stackoverflow.com/questions/1025837/initialize-java-generic-array-of-type-generic) – Eric Mar 26 '13 at 12:08
  • 1
    Please do a thorough search on SO for your questions before asking them http://stackoverflow.com/questions/217065/cannot-create-an-array-of-linkedlists-in-java – Code2Interface Mar 26 '13 at 12:29

6 Answers6

4

NPE: "Arrays and generics don't go well together"

Go for list of lists

List<List<Integer>> a = new ArrayList<List<Integer>>();

or double array

int[][] a = new int[5][5];
Nikolay Kuznetsov
  • 8,896
  • 9
  • 49
  • 94
3

You can create an array of lists but you cannot use type during initalization.

List<Integer>[] lists=new List[10];

//insertion
for(int i=0;i<10;i++){
    lists[i]=new ArrayList<Integer>();
    lists[i].add(i);
    lists[i].add(i+1);
}

//printing
for(List<Integer> list:lists){
    System.out.println(list.size());
}

Why this works? Because the lists variable points to an array data structure whose data type is List<Integer>. This array contains a set of references to different objects of type List<Integer>, and this is why if we try to run lists[i]=new ArrayList<String>(); it will not compile. However when we initialize the array itself we don't need to provide the type of the List objects as List since from JVM point of view a List of Integer objects and a List of Object objects will require the same number of bytes as logn as their sizes is same. The only constraint comes when we set a array member to a value (of type List - it has to be List<Integer> not anything else)

You can type cast the List[] to a List<Integer>[] but the end result and the JVM behavior is the same.

Jit B
  • 1,104
  • 10
  • 25
2

This works

int arraySize = 10;
List<Integer>[] a = (List<Integer>[]) new List[arraySize];

It create an array (of size 10) that can contains List of Integer

ben75
  • 27,769
  • 7
  • 80
  • 130
  • Don't bother reading the initial comments as they refer to an incorrect link and people asking why why why. This worked perfectly, simple question and simple answer. I was grouping my four List together and an array was the perfect choice. Good answer. – xchiltonx Oct 05 '13 at 14:44
0
a = (List<Integer>[]) new List[num];
for(int i = 0; i < num; i++)
    a[i] = new ArrayList<Integer>();

Really though, why not declare a as a List<List<Integer>>?

ben75
  • 27,769
  • 7
  • 80
  • 130
Eric
  • 87,154
  • 48
  • 211
  • 332
0

you can do as List<Integer> a=new ArrayList<Integer>(); but a should not be generic

kkh
  • 31
  • 4
-3

List is an interface in java!

you can give references of instances like ArrayList, linket list, vector and stack to a variable with List datatype

so you can use one of the following,

a = new ArrayList<Integer>();
a = new LinkedList<Integer>();
a = new Vector()<Integer>;
a = new Stack()<Integer>;
Nomesh Gajare
  • 845
  • 2
  • 11
  • 28