How to create an LinkedList<Object[]>[]?
What would be the synt开发者_运维百科ax to create a LinkedList<Object[]>[]
type variable?
I have tried:
public LinkedList<Object[]>[] myList = new LinkedList<Object[]>()[];
but this doesn't work.
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[]>>();
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
tomyArray
, 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 toLinkedList
.
So the line would look like this:
public List<Object[]>[] myArray = new List[ARRAY_SIZE];
精彩评论