【问题标题】:Dart can't find index of object in list with indexOf()Dart 无法使用 indexOf() 在列表中找到对象的索引
【发布时间】:2021-08-25 10:08:17
【问题描述】:

所以,要么我手头有一个非常奇怪的问题,要么我就是个笨蛋。

我有一个对象列表。每个对象的 Attributes 是几个 double,一个 int 和一个 String。

我的问题是 list.indexOf(object) 返回 -1(例如 indexOf() 找不到元素),即使列表中有一个对象,其属性是我要查找其索引的对象的副本。

即使list.indexOf(list[0]) 返回 -1,出于某种该死的原因(编辑:最初将 -1 错误输入为 0)。是的,列表中实际上包含数据。

这是我的对象的代码:

class Standard {
  double l;
  double a;
  double b;
  double lUpperTolerance;
  double lLowerTolerance;
  double aUpperTolerance;
  double aLowerTolerance;
  double bUpperTolerance;
  double bLowerTolerance;
  double eTolerance;
  int timestamp;
  String identStr;

  Standard(
      {this.l,
      this.a,
      this.b,
      this.lUpperTolerance,
      this.lLowerTolerance,
      this.aUpperTolerance,
      this.aLowerTolerance,
      this.bUpperTolerance,
      this.bLowerTolerance,
      this.eTolerance,
      this.timestamp,
      this.identStr});

  factory Standard.fromJson(Map<String, dynamic> jsonData) {
    if (jsonData != null) {
      double l = (jsonData['l'] ?? 0.0) as double;
      double a = (jsonData['a'] ?? 0.0) as double;
      double b = (jsonData['b'] ?? 0.0) as double;
      double lUpperTolerance = (jsonData['lUpperTolerance'] ?? 1.0) as double;
      double lLowerTolerance = (jsonData['lLowerTolerance'] ?? 1.0) as double;
      double aUpperTolerance = (jsonData['aUpperTolerance'] ?? 1.0) as double;
      double aLowerTolerance = (jsonData['aLowerTolerance'] ?? 1.0) as double;
      double bUpperTolerance = (jsonData['bUpperTolerance'] ?? 1.0) as double;
      double bLowerTolerance = (jsonData['bLowerTolerance'] ?? 1.0) as double;
      double eTolerance = (jsonData['eTolerance'] ?? 1.0) as double;
      int timestamp = (jsonData['timestamp'] ?? 1500000000) as int;
      String identStr = (jsonData['identStr'] ?? '0') as String;
      return Standard(
          l: l,
          a: a,
          b: b,
          lUpperTolerance: lUpperTolerance,
          lLowerTolerance: lLowerTolerance,
          aUpperTolerance: aUpperTolerance,
          aLowerTolerance: aLowerTolerance,
          bUpperTolerance: bUpperTolerance,
          bLowerTolerance: bLowerTolerance,
          eTolerance: eTolerance,
          timestamp: timestamp,
          identStr: identStr);
    } else {
      return null;
    }
  }

  Map<String, dynamic> toJson() => {
        'l': l,
        'a': a,
        'b': b,
        'lUpperTolerance': lUpperTolerance,
        'lLowerTolerance': lLowerTolerance,
        'aUpperTolerance': aUpperTolerance,
        'aLowerTolerance': aLowerTolerance,
        'bUpperTolerance': bUpperTolerance,
        'bLowerTolerance': bLowerTolerance,
        'eTolerance': eTolerance,
        'timestamp': timestamp,
        'identStr': identStr,
      };

  void dump() {
    print('l: ' + this.l.toString());
    print('a: ' + this.a.toString());
    print('b: ' + this.b.toString());
    print('lUpper: ' + this.lUpperTolerance.toString());
    print('lLower: ' + this.lLowerTolerance.toString());
    print('aUpper: ' + this.aUpperTolerance.toString());
    print('aLower: ' + this.aLowerTolerance.toString());
    print('bUpper: ' + this.bUpperTolerance.toString());
    print('bLower: ' + this.bLowerTolerance.toString());
    print('eTolerance: ' + this.eTolerance.toString());
    print('timestamp: ' + this.timestamp.toString());
    print('identStr: ' + this.identStr.toString());
  }
}

【问题讨论】:

  • 你能显示你的对象的代码吗?您确定没有覆盖对象中的 == 运算符吗?
  • @smotastic 将其添加到问题的末尾。除了我想用它来查找对象索引的位置之外,我在任何代码中都找不到任何提及 indexOf 的内容,并且 indexOf 上的 Ctrl + Q 显示了来自list.dart的帮助
  • indexOf 取决于对象相等性。如果您没有为您的对象提供operator ==,那么默认的相等概念就是对象标识。但是,list.indexOf(list[0]) 不应该返回 -1,如果那是您真正输入的内容。请提供演示您遇到的问题的重现代码。
  • 因为我不太确定我是否真的偶然发现了这里可能被视为错误的地方,所以我实际上将那行确切的行放入了我的代码中并运行了它。但是,是的,它缺少一个相等运算符。

标签: list dart indexof


【解决方案1】:

最后结果是我需要添加一个方法来比较对象,所以我的类最终看起来像这样:

class Standard {
  double l;
  double a;
  double b;
  double lUpperTolerance;
  double lLowerTolerance;
  double aUpperTolerance;
  double aLowerTolerance;
  double bUpperTolerance;
  double bLowerTolerance;
  double eTolerance;
  int timestamp;
  String identStr;

  Standard({this.l,
    this.a,
    this.b,
    this.lUpperTolerance,
    this.lLowerTolerance,
    this.aUpperTolerance,
    this.aLowerTolerance,
    this.bUpperTolerance,
    this.bLowerTolerance,
    this.eTolerance,
    this.timestamp,
    this.identStr});

  @override
  bool operator ==(o) {
    if (o is! Standard) {
      return false;
    } else {
      return o.l == l &&
          o.a == a &&
          o.b == b &&
          o.lUpperTolerance == lUpperTolerance &&
          o.lLowerTolerance == lLowerTolerance &&
          o.aUpperTolerance == aUpperTolerance &&
          o.aLowerTolerance == aLowerTolerance &&
          o.bUpperTolerance == bUpperTolerance &&
          o.bLowerTolerance == bLowerTolerance &&
          o.eTolerance == eTolerance &&
          o.timestamp == timestamp &&
          o.identStr == identStr;
    }
  }

  @override
  int get hashCode => super.hashCode;

  factory Standard.fromJson(Map<String, dynamic> jsonData) {
    if (jsonData != null) {
      double l = (jsonData['l'] ?? 0.0) as double;
      double a = (jsonData['a'] ?? 0.0) as double;
      double b = (jsonData['b'] ?? 0.0) as double;
      double lUpperTolerance = (jsonData['lUpperTolerance'] ?? 1.0) as double;
      double lLowerTolerance = (jsonData['lLowerTolerance'] ?? 1.0) as double;
      double aUpperTolerance = (jsonData['aUpperTolerance'] ?? 1.0) as double;
      double aLowerTolerance = (jsonData['aLowerTolerance'] ?? 1.0) as double;
      double bUpperTolerance = (jsonData['bUpperTolerance'] ?? 1.0) as double;
      double bLowerTolerance = (jsonData['bLowerTolerance'] ?? 1.0) as double;
      double eTolerance = (jsonData['eTolerance'] ?? 1.0) as double;
      int timestamp = (jsonData['timestamp'] ?? 1500000000) as int;
      String identStr = (jsonData['identStr'] ?? '0') as String;
      return Standard(
          l: l,
          a: a,
          b: b,
          lUpperTolerance: lUpperTolerance,
          lLowerTolerance: lLowerTolerance,
          aUpperTolerance: aUpperTolerance,
          aLowerTolerance: aLowerTolerance,
          bUpperTolerance: bUpperTolerance,
          bLowerTolerance: bLowerTolerance,
          eTolerance: eTolerance,
          timestamp: timestamp,
          identStr: identStr);
    } else {
      return null;
    }
  }

  Map<String, dynamic> toJson() =>
      {
        'l': l,
        'a': a,
        'b': b,
        'lUpperTolerance': lUpperTolerance,
        'lLowerTolerance': lLowerTolerance,
        'aUpperTolerance': aUpperTolerance,
        'aLowerTolerance': aLowerTolerance,
        'bUpperTolerance': bUpperTolerance,
        'bLowerTolerance': bLowerTolerance,
        'eTolerance': eTolerance,
        'timestamp': timestamp,
        'identStr': identStr,
      };

  void dump() {
    print('l: ' + this.l.toString());
    print('a: ' + this.a.toString());
    print('b: ' + this.b.toString());
    print('lUpper: ' + this.lUpperTolerance.toString());
    print('lLower: ' + this.lLowerTolerance.toString());
    print('aUpper: ' + this.aUpperTolerance.toString());
    print('aLower: ' + this.aLowerTolerance.toString());
    print('bUpper: ' + this.bUpperTolerance.toString());
    print('bLower: ' + this.bLowerTolerance.toString());
    print('eTolerance: ' + this.eTolerance.toString());
    print('timestamp: ' + this.timestamp.toString());
    print('identStr: ' + this.identStr.toString());
  }
}

【讨论】:

  • 这里的hashCode只是使用了Object.hashCode,和声明的==不兼容,所以可以把一个对象放到HashSet里面,如果查找就找不到具有相等但不相同的值。
  • 这是否解释了为什么这也不起作用? list.indexOf(list[0]) 默认应该是寻找相同的引用。这应该在列表中找到与您在列表中搜索第一个对象相同的引用?
  • 我不知道为什么这不起作用,但这解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-06
相关资源
最近更新 更多