如何在 Java 中正确定义链表数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27654229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly define an array of linked list in Java ?
提问by mynameisJEFF
I tried to define an array of linked list in Java like the following, which compiled fine but it generated 2 warning messages.
我尝试在 Java 中定义一个链表数组,如下所示,它编译得很好,但它生成了 2 条警告消息。
LinkedList<Long> [] hashtable = new LinkedList[10];
warning: [rawtypes] found raw type: LinkedList
LinkedList<Long> [] hashtable = new LinkedList[10];
^
missing type arguments for generic class LinkedList<E>
where E is a type-variable:
E extends Object declared in class LinkedList
HashTable.java:13: warning: [unchecked] unchecked conversion
LinkedList<Long> [] hashtable = new LinkedList[10];
^
required: LinkedList<Long>[]
found: LinkedList[]
So, I tried
所以,我试过了
LinkedList<Long> [] hashtable = new LinkedList<Long>[10];
But this time it would not even compile and generate this error instead.
但这一次它甚至不会编译并生成此错误。
HashTable.java:13: error: generic array creation
LinkedList<Long> [] hashtable = new LinkedList<Long>[10];
^
1 error
So, how should I define my array of linked list properly ?
那么,我应该如何正确定义我的链表数组?
采纳答案by bedrin
This is a proper way to create an array:
这是创建数组的正确方法:
@SuppressWarnings("unchecked") LinkedList<Long> [] hashtable = new LinkedList[10];
Cannot Create Arrays of Parameterized Types
无法创建参数化类型的数组
You cannot create arrays of parameterized types. For example, the following code does not compile:
您不能创建参数化类型的数组。例如,以下代码无法编译:
List<Integer>[] arrayOfLists = new List<Integer>[2]; // compile-time error
The following code illustrates what happens when different types are inserted into an array:
以下代码说明了将不同类型插入数组时会发生什么:
Object[] strings = new String[2];
strings[0] = "hi"; // OK
strings[1] = 100; // An ArrayStoreException is thrown.
If you try the same thing with a generic list, there would be a problem:
如果你用通用列表尝试同样的事情,就会出现问题:
Object[] stringLists = new List<String>[]; // compiler error, but pretend it's allowed
stringLists[0] = new ArrayList<String>(); // OK
stringLists[1] = new ArrayList<Integer>(); // An ArrayStoreException should be thrown,
// but the runtime can't detect it.
If arrays of parameterized lists were allowed, the previous code would fail to throw the desired ArrayStoreException
.
如果允许参数化列表的数组,则前面的代码将无法抛出所需的ArrayStoreException
.
Taken from docs.oracle.com
So what can I store in hashtable[] ?
那么我可以在 hashtable[] 中存储什么?
Does it mean I am now allowed to have a linked list of string in the hashtable[0] and a linked list of Long in hashtable1, if I do LinkedList [] hashtable = new LinkedList[10]?
如果我执行 LinkedList [] hashtable = new LinkedList[10],这是否意味着我现在可以在 hashtable[0] 中有一个 string 的链表,在 hashtable 1 中有一个 Long 的链表?
No, compiler won't allow you to store LinkedList to the hashtable array directly. Following snippet won't compile:
不,编译器不允许您直接将 LinkedList 存储到哈希表数组。以下代码段将无法编译:
hashtable[0] = new LinkedList<String>();
However you can store the LinkedList without type parameters, or even a subclass of LinkedList:
但是,您可以存储不带类型参数的 LinkedList,甚至可以存储 LinkedList 的子类:
@SuppressWarnings("unchecked") LinkedList<Long>[] hashtable = new LinkedList[10];
hashtable[0] = new LinkedList<Long>();
hashtable[1] = new MyLinkedList<Long>();
hashtable[2] = new LinkedList();
hashtable[3] = new MyLinkedList();
You can store the LinkedList if you cast your array to LinkedList[]. However you won't be able to store the anything else but a LinkedList:
如果将数组转换为 LinkedList[],则可以存储 LinkedList。但是,除了 LinkedList 之外,您将无法存储其他任何内容:
LinkedList[] rawHashTable = hashtable;
rawHashTable[4] = new LinkedList<String>();
Object[] objectHashTable = rawHashTable;
objectHashTable[5] = "This line will throw an ArrayStoreException ";
回答by ZakiMak
If you need a list/array of LinkedList, you could use an ArrayList to hold the collection with an initial size of 10.
如果您需要 LinkedList 的列表/数组,则可以使用 ArrayList 来保存初始大小为 10 的集合。
Here is an alternative approach you could try:
这是您可以尝试的替代方法:
ArrayList<LinkedList<Long>> list = new ArrayList<LinkedList<Long>>(10);
回答by Tahmid Nips
First of all define the array size where each element is a LinkedList.
首先定义数组大小,其中每个元素都是一个 LinkedList。
LinkedList<Long> hashTable[] = new LinkedList[10];
Now since each element in the array is a LinkedList
itself and all of them are null
each of them needs to be initialized. Hence,
现在,由于数组中的每个元素都是一个LinkedList
本身,并且所有元素null
都需要初始化。因此,
for (int i=0;i<10;i++)
hashTable[i] = new LinkedList<Long>();
If you want to add data to a list, then do it like this:
如果要将数据添加到列表中,请执行以下操作:
hashTable[i].add(YOUR_LONG_DATA_HERE);
and finally to iterate,
最后迭代,
for (int i=0;i<10;i++){
for (Long j: hashTable[i])
System.out.println(j);
}