list - DART:实例列表中的 indexOf()

标签 list dart indexof

如何获取List中一个Instance的索引?

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');
}

最佳答案

  // some helper to satisfy `firstWhere` when no element was found
  var dummy = new Point(null, null);
  var p = pts.firstWhere((e) => e.x == 25, orElse: () => dummy);
  if(p != dummy) {
    // don't know if this is still relevant to your question
    // the lines above already got the element 
    var lx = pts[pts.indexOf(p)];
    print('x: ${lx.x}, y: ${lx.y}');
  }

关于list - DART:实例列表中的 indexOf(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28501086/

相关文章:

javascript - 为什么 indexOf 在数组 IE8 上不起作用?

javascript - 原子索引错误与从视频 currentTime 派生的 searchElement

python - 使用 input() 列出的数字

r - 构造一个命名列表而不必键入每个对象的名称两次

python - 使用列表来收集元素

javascript - 在 Dart 中创建 Cesium 图像提供程序

dart - 如何模拟按键事件?

python - 从Python字典中提取id作为列表

spring - Dart MIME类型在 Spring 之前更改为vnd.dart

javascript - document.location.indexOf 不是一个函数