【发布时间】:2016-10-08 04:52:14
【问题描述】:
我有一个名为 SparseMatrix 的类。它包含一个 ArrayList 节点(也是类)。我想知道如何遍历数组并访问 Node.js 中的值。我尝试了以下方法:
//Assume that the member variables in SparseMatrix and Node are fully defined.
class SparseMatrix {
ArrayList filled_data_ = new ArrayList();
//Constructor, setter (both work)
// The problem is that I seem to not be allowed to use the operator[] on
// this type of array.
int get (int row, int column) {
for (int i = 0; i < filled_data_.size(); i++){
if (row * max_row + column == filled_data[i].getLocation()) {
return filled_data[i].getSize();
}
}
return defualt_value_;
}
}
我可能会切换到静态数组(并在每次添加对象时重新制作)。如果有人有解决方案,我将非常感谢您与我分享。另外,提前感谢您对我的帮助。
如果您不明白这里的任何内容,请随时提出问题。
【问题讨论】:
-
你应该使用泛型,你不能使用 [i] 从 ArrayList 中获取元素,你必须使用 .get(i)。