问题
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.
回答1:
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[]>>();
回答2:
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
myListtomyArray, 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];
来源:https://stackoverflow.com/questions/6961783/how-to-create-an-linkedlistobject