2

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

5
  • each element of the array is a list. Commented Apr 27, 2015 at 21:24
  • 2
    I think you might need to rephrase your question. Most notably, the code extract you supplied is not accurate. You cannot instantiate arrays of generic type. Commented Apr 27, 2015 at 21:24
  • You should be able to make an array of arraylists, it is a calss like any other Commented Apr 27, 2015 at 21:25
  • you could also make a 2 dimensional array of ints Commented Apr 27, 2015 at 21:31
  • @JoshuaByer No, ArrayList is parameterized (generic), and therefore you cannot make arrays with it. Commented Apr 27, 2015 at 21:36

9 Answers 9

7

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<>();
Sign up to request clarification or add additional context in comments.

Comments

4

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>

Comments

3

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

1 Comment

@Martin M J I dont think this answers my question though. What i want is an array of lists.
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.

1 Comment

You can't new ArrayList<Integer>[10] but you can declare a variable as List<Integer>[] intListArray. So List<Integer>[] intListArray = new ArrayList[10]; creates an array which can hold only List<Integer>. (see example)
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

Comments

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.

Comments

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.

Comments

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

Comments

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

Comments

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.