1

I have an array. For each element of the array, I want to store multiple integers. I know that in C I can make an array of integer pointers, and use that pointer to make a list.

In Java, I can make an array of object 'A', where A has a list of integers. But why can't I do something like this

List<Integer>[] arr = new ArrayList<Integer>[]();

I get:

Type mismatch: cannot convert from ArrayList to List[]

Makoto
  • 96,408
  • 24
  • 164
  • 210
Kraken
  • 20,468
  • 32
  • 90
  • 145

9 Answers9

6

You typically want to avoid mixing collections and arrays together for this very reason; an array is covariant (that is, Integer[] is-an Object[]), but collections (and generics in general) are invariant (that is, a List<Integer> is not a List<Object>).

You can definitely create a list of lists instead, which will ensure type safety and get you around the issue of creating a generic array:

List<List<Integer>> intNestedList = new ArrayList<>();
Makoto
  • 96,408
  • 24
  • 164
  • 210
3

As stated in Java's own documentation, you cannot create an array of generics.

Martin M J
  • 770
  • 1
  • 4
  • 12
  • @Martin M J I dont think this answers my question though. What i want is an array of lists. – Kraken Apr 27 '15 at 21:25
3

If you want to create an array which can hold up to ten List<Integer> you must declare the array that way.

List<Integer>[] arr = new ArrayList[10];

following assignment is valid

List<Integer> intList = new ArrayList<>();
arr[0] = intList;

whereas following will fail with an compilation error

List<String> stringList = new ArrayList<>();
arr[0] = stringList;

the compilation fails with

incompatible types: java.util.List<java.lang.String>
   cannot be converted to java.util.List<java.lang.Integer>
SubOptimal
  • 20,523
  • 3
  • 44
  • 57
2

An ArrayList is a List, but an ArrayList is not a List[]

If you want an Array of Lists that hold integers, I would suggest:

List<Integer>[] xyz;  // still writing code will update in a sec

It turns out you can't create arrays of parameterized types, according to the oracle docs.

Aify
  • 3,393
  • 3
  • 23
  • 41
  • You can't `new ArrayList[10]` but you can declare a variable as `List[] intListArray`. So `List[] intListArray = new ArrayList[10];` creates an array which can hold only `List`. (see [example](https://stackoverflow.com/questions/29906131/defining-an-array-of-list-of-integers-in-java/29906580#29906580)) – SubOptimal Apr 28 '15 at 06:28
1

Unless you know for sure you want a finite array, I suggest you do something like List<List<Integer>> arr = new ArrayList<List<Integer>>();

If you really want an array of Lists then you'll want to see this Java question about ArrayList<Integer>[] x

Community
  • 1
  • 1
Transcendence
  • 2,226
  • 2
  • 18
  • 31
1

Creating an array of List is no different than creating an array of any other object. You can do any of the following:

List[] listsArray = new List[3];
listsArray[0] = new ArrayList();
listsArray[1] = new LinkedList();
listsArray[2] = new ArrayList();

Or:

List[] listsArray = new List[]{new ArrayList(), new LinkedList(), new ArrayList()};

Note that you are limited in what you can do with generics on arrays.

Community
  • 1
  • 1
Brett Okken
  • 5,929
  • 1
  • 17
  • 24
1

Not a very nice solution but you might try it with a cast. Something like this:

List<Integer>[] arr = (List<Integer>[]) new List[SIZE_OF_YOUR_ARRAY];

You will probably get a warning but it should still work.

b.geisb
  • 11
  • 2
0

As i found, you need an array of arrays. you can do this, to make your inner arrays:

Integer[] array1 = new Integer[];
Integer[] array2 = new Integer[];

and then put them in another array like this:

Integer[][] arrays = new Integer[][] { array1, array2 };

or

Integer[][] arrays = { array1, array2 };

maybe it's better to do it like this:

List<List<Integer>> listOfLists = Lists.newArrayList();
listOfLists.add(Lists.newArrayList("123","456","789"));

after all I recommend you to read this: How to make an array of arrays in Java

Community
  • 1
  • 1
simin
  • 19
  • 6
0

Graph Implementation using Adjacency List depicts the usage of an Array of List.

 public class Graph {
    int vertex;
    LinkedList<Integer> list[];

    public Graph(int vertex) {
        this.vertex = vertex;
        list = new LinkedList[vertex];
        for (int i = 0; i <vertex ; i++) {
            list[i] = new LinkedList<>();
        }
    }
 }

As you can observe that the constructor of class Graph, is used to define the Array of List. in the same constructor, Array Of List is initialized too. Hope It would be Helpful to resolve your problem and requirement !.

Avi
  • 3,075
  • 8
  • 17
  • 27