0

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.

pqn
  • 1,522
  • 3
  • 22
  • 33

2 Answers2

2

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[]>>();
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
1

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];
Community
  • 1
  • 1
Eli Acherkan
  • 6,401
  • 2
  • 27
  • 34