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

醉酒当歌 提交于 2019-12-11 15:08:07

问题


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


来源:https://stackoverflow.com/questions/6961783/how-to-create-an-linkedlistobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!