我有一个名为SparseMatrix的类。它包含节点的ArrayList(也是类)。我想知道如何迭代Array并访问Node中的值。我试过以下内容:如何遍历对象的ArrayList?
//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)。 –