如何在列表中获取一个实例的索引?
class Points {
int x, y;
Point(this.x, this.y);
}
void main() {
var pts = new List();
int Lx;
int Ly;
Points pt = new Points(25,55); // new instance
pts.add(pt);
int index = pts.indexOf(25); // Problem !!! How to obtain the index in a list of instances ?
if (index != -1 ){
Lx = lp1.elementAt(index).x;
Ly = lp1.elementAt(index).y;
print('X=$Lx Y=$Ly');
}
pts.indexOf(25)
应该返回什么?第一个元素是x
或y
等于25
吗? - Günter Zöchbauermap.values
一样访问该Map,但通过x值查找Point会更快(Gunter的答案是O(n),而Map将在O(log n)时间内找到Point)。 - Michael Fenwick