pqn
pqn

Reputation: 1532

How to create an LinkedList<Object[]>[]?

What would be the syntax to create a LinkedList<Object[]>[] type variable?

I have tried:

public LinkedList<Object[]>[] myList = new LinkedList<Object[]>()[];

but this doesn't work.

Upvotes: 0

Views: 2177

Answers (2)

Eli Acherkan
Eli Acherkan

Reputation: 6411

The declaration LinkedList<Object[]>[] means an array of lists of arrays - is that the intention? Assuming that it is, you create it with the syntax for creating arrays:

public LinkedList<Object[]>[] myArray = new LinkedList[ARRAY_SIZE];

This creates an array of the specified size (ARRAY_SIZE), each cell of which is null.

Note that:

  • Since you can't create generic arrays in Java, as Hunter McMillen noticed, the right part omits the type of the LinkedList (i.e. "<Object[]>").
  • I took the liberty of renaming the variable from myList to myArray, since it's an array and not a list.
  • It's usually a good idea to use the interface (List) and not a specific implementation (LinkedList), unless you need to use methods specific to LinkedList.

So the line would look like this:

public List<Object[]>[] myArray = new List[ARRAY_SIZE];

Upvotes: 1

Hunter McMillen
Hunter McMillen

Reputation: 61515

In Java you can't create generic arrays. You can however do this with ArrayList class or any class that implements the List interface.

List<LinkedList<Object[]>> myList = new ArrayList<LinkedList<Object[]>>();

Upvotes: 2

Related Questions