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

How should i new the a ?

6
  • 7
    Arrays and generics don't go well together. Commented Mar 26, 2013 at 12:00
  • what is a? is it a array of List<Integer> Commented Mar 26, 2013 at 12:00
  • Why would you even need such a construct? (apart from an interview or exam question...) Commented Mar 26, 2013 at 12:03
  • You cannot create an array of a parameterised type in Java. See: stackoverflow.com/questions/3903196/… Commented Mar 26, 2013 at 12:08
  • 1
    Please do a thorough search on SO for your questions before asking them stackoverflow.com/questions/217065/… Commented Mar 26, 2013 at 12:29

6 Answers 6

5

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.

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

3 Comments

why would someone need this mess?
In production level java nobody would require this (we are all about Collections and Collections of Collections) but it's good food for thought.
nobody uses arrays unless they 'need' to work with primitives. arrays in java might have been useful if they were kept in stack.
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];

Comments

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

1 Comment

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<Integer> together and an array was the perfect choice. Good answer.
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>>?

Comments

0

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

Comments

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

3 Comments

Look closely at the question. a is an array of lists. Java does not allow you to make an array of a parameterised type.
yes you are right jasper! i did not observe keenly!
And what is with the syntax on the last two?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.