如何正确地定义链表的Java中的数组? [英] How to properly define an array of linked list in Java ?

查看:244
本文介绍了如何正确地定义链表的Java中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java来定义链表的数组像下面这样,它编译罚款,但它产生的2警告消息。

 的LinkedList<龙> [] =哈希表新的LinkedList [10];警告:[rawtypes]研究发现原始类型:LinkedList的
    LinkedList的<龙> [] =哈希表新的LinkedList [10];
                                        ^
  缺少类型参数的泛型类LinkedList的< E>
  其中E是一个类型的变量:
    Ë扩展了LinkedList类申报对象
HashTable.java:13:警告:[选中]选中的转换
    LinkedList的<龙> [] =哈希表新的LinkedList [10];
                                    ^
  要求:LinkedList的<龙> []
  发现:LinkedList的[]

所以,我试过

 的LinkedList<龙> [] =哈希表新的LinkedList<龙> [10];

但是,这一次它甚至不会编译生成该错误信息。

  HashTable.java:13:错误:通用数组创建
    LinkedList的<龙> [] =哈希表新的LinkedList<龙> [10];
                                    ^
1个错误

那么,应该怎么正确定义我的链接列表的数组?


解决方案

这是创建一个数组以适当方式:

  @燮pressWarnings(未登记)的LinkedList<龙> [] =哈希表新的LinkedList [10];

无法创建参数化类型的数组

您不能创建参数化类型的数组。例如,下面的code不能编译:

 列表<整数GT; [] = arrayOfLists新的List<整数GT; [2]; //编译时错误

以下code说明了当不同类型的被插入到一个数组会发生什么:

 对象[] =串新的String [2];
串[0] =HI; // 好
串[1] = 100; //则抛出ArrayStoreException异常。

如果你尝试用一个通用的列表同样的事情,就不会有一个问题:

 对象[] = stringLists新的List<串GT; []; //编译器错误,但pretend它允许
stringLists [0] =新的ArrayList<串GT;(); // 好
stringLists [1] =新的ArrayList&所述;整数>(); //一个ArrayStoreException信息应该被抛出,
                                            //但是运行时无法检测到它。

如果参数列表的数组被允许,在previous code将无法抛出所需的 ArrayStoreException信息

docs.oracle.com <拍摄/ p>

那么,我可以在哈希表[]存储?


  

这是否意味着我现在允许有串在一个链表
  散列表[0]和长的在散列表 1 ,如果我做
  LinkedList的[]哈希表=新的LinkedList [10]?


没有,编译器不会让你的LinkedList直接存储到哈希表数组。下面的代码片段将不会编译:

 哈希表[0] =新的LinkedList&LT;串GT;();

不过,你可以存储LinkedList的无类型参数,或LinkedList的,甚至一个子类:

  @燮pressWarnings(未登记)的LinkedList&LT;龙&GT; [] =哈希表新的LinkedList [10];哈希表[0] =新的LinkedList&LT;龙&GT;();
哈希表[1] =新MyLinkedList&LT;龙&GT;();
哈希表[2] =新链表();
哈希表[3] =新MyLinkedList();

您可以存储LinkedList的,如果你投你的阵列LinkedList的[]。然而,你将无法保存任何东西,但在LinkedList:

  LinkedList的[] rawHashTable =哈希表;
rawHashTable [4] =新链表&所述;字符串&GT;();[对象] objectHashTable = rawHashTable;
objectHashTable [5] =此行会抛出一个ArrayStoreException信息;

I tried to define an array of linked list in Java like the following, which compiled fine but it generated 2 warning messages.

 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 ?

解决方案

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.

Taken from docs.oracle.com

So what can I store in 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]?

No, compiler won't allow you to store LinkedList to the hashtable array directly. Following snippet won't compile:

hashtable[0] = new LinkedList<String>();

However you can store the LinkedList without type parameters, or even a subclass of 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[] rawHashTable = hashtable;
rawHashTable[4] = new LinkedList<String>();

Object[] objectHashTable = rawHashTable;
objectHashTable[5] = "This line will throw an ArrayStoreException ";

这篇关于如何正确地定义链表的Java中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆