无法建立在Java中LinkedLists数组...? [英] Cannot create an array of LinkedLists in Java...?
问题描述
我的工作了稀疏矩阵类需要使用的LinkedList
的数组存储矩阵的值。数组的每个元素(即每个的LinkedList
)再presents矩阵的一行。而且,在的LinkedList
阵列重新$ P $每个元素psents一列和存储的值。
I'm working on a sparse matrix class that needs to use an array of LinkedList
to store the values of a matrix. Each element of the array (i.e. each LinkedList
) represents a row of the matrix. And, each element in the LinkedList
array represents a column and the stored value.
在我的课,我有一个数组的声明如下:
In my class, I have a declaration of the array as:
private LinkedList<IntegerNode>[] myMatrix;
和,在我的构造方法稀疏矩阵
,我尝试定义:
And, in my constructor for the SparseMatrix
, I try to define:
myMatrix = new LinkedList<IntegerNode>[numRows];
我最终得到的误差
The error I end up getting is
无法创建一个通用的阵列的LinkedList&LT; IntegerNode方式&gt;
Cannot create a generic array of
LinkedList<IntegerNode>
.
所以,我有两个问题与此:
So, I have two issues with this:
- 我在做什么错了,
- 为什么类型的声明数组可以接受的,如果它不能创造?
IntegerNode
是我创建了一个类。而且,我所有的类文件打包在一起。
IntegerNode
is a class that I have created. And, all of my class files are packaged together.
推荐答案
您无法使用一般的阵列。这是Java泛型的缺陷/功能。
You can't use generic array creation. It's a flaw/ feature of java generics.
没有警告的方法是:
-
使用列表,而不是列表的数组列表:
Using List of Lists instead of Array of Lists:
List< List<IntegerNode>> nodeLists = new LinkedList< List< IntegerNode >>();
声明的特殊类列表的数组:
Declaring the special class for Array of Lists:
class IntegerNodeList {
private final List< IntegerNode > nodes;
}
这篇关于无法建立在Java中LinkedLists数组...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!