无法在 Java 中创建 LinkedLists 数组...? [英] Cannot create an array of LinkedLists in Java...?
问题描述
我正在研究一个稀疏矩阵类,它需要使用 LinkedList
数组来存储矩阵的值.数组的每个元素(即每个 LinkedList
)代表矩阵的一行.并且,LinkedList
数组中的每个元素代表一列和存储的值.
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;
而且,在我的 SparseMatrix
构造函数中,我尝试定义:
And, in my constructor for the SparseMatrix
, I try to define:
myMatrix = new LinkedList<IntegerNode>[numRows];
我最终得到的错误是
无法创建 LinkedList
的通用数组.
Cannot create a generic array of
LinkedList<IntegerNode>
.
所以,我有两个问题:
- 我做错了什么,以及
- 如果无法创建数组,为什么在声明中可以接受该类型?
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屋!