Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 无法理解DART中的列表考虑所提供的示例_List_Algorithm_Flutter_Dart - Fatal编程技术网

List 无法理解DART中的列表考虑所提供的示例

List 无法理解DART中的列表考虑所提供的示例,list,algorithm,flutter,dart,List,Algorithm,Flutter,Dart,我正在开发一个应用程序在颤振。我正在使用地图列表,但有些东西我无法理解。考虑以下情况: 情景1 void main() { List<Map<String,String>> _reminders = []; Map<String , String> _tempMap = {}; for (int i = 0; i < 5; i++) { _tempMap.clear(); _tempMap.putIfAbsent('M'

我正在开发一个应用程序在颤振。我正在使用地图列表,但有些东西我无法理解。考虑以下情况:

  • 情景1

    void main() {
      List<Map<String,String>> _reminders = [];
      Map<String , String> _tempMap = {};
    
      for (int i = 0; i < 5; i++) {
        _tempMap.clear();
        _tempMap.putIfAbsent('M' , () => 'm ' + i.toString());
        _tempMap.putIfAbsent('D' , () => 'd : ' + i.toString());
    
        _reminders.add(_tempMap);
        // or _reminders.insert(i, _tempMap);
      }
      print(_reminders.toString());
      return;
    }
    
    • 场景2

      void main() {
        List<Map<String,String>> _reminders = [];
      
      
        for (int i = 0; i < 5; i++) {
          Map<String , String> _tempMap = {};
          _tempMap.putIfAbsent('M' , () => 'm ' + i.toString());
          _tempMap.putIfAbsent('D' , () => 'd : ' + i.toString());
      
          _reminders.add(_tempMap);;
        }
        print(_reminders.toString());
        return;
      }
      
      据我所知,这些场景应该会产生类似的结果。在我的用例中,场景2也是正确的方法,因为它给了我想要的结果。请注意,上述示例已更改为类似问题。我的原始代码中的用法要复杂得多

      据我了解,两者都不一样

      问题在于
      \u tempMap.clear()。您已经为map对象使用了全局变量,当您在for循环中应用clear时,以前添加的所有条目都将被清除,map将变为空

      当i=0=>{}=>clear()=>时,所有条目都将被清除=>插入新项

      当i=1=>{“在第0次迭代中插入的项”}=>clear()=>将清除所有项=>插入新项

      所以对于每个迭代,映射都被清除,并且只保存最后一次迭代的值。for循环完成后,它只包含最后一个迭代值(i=4),因为每次新迭代开始时,我们都会清除全局map变量

      编辑: 您可以在for循环中打印映射值,并可以自己检查

      for (int i = 0; i < 5; i++) {
          print('\n $i => ${_tempMap} \n');
      
       
      
      for(int i=0;i<5;i++){
      打印('\n$i=>${u tempMap}\n');
      
      Dart与包括java在内的许多其他编程语言一样,将对象存储为引用,而不是连续的内存块。在第一种情况下,在循环的所有迭代中,您使用
      \u提醒添加了相同的
      映射。添加(\u tempMap)
      。您的直觉是“每次我添加
      映射
      ,都会创建当前状态的
      映射
      副本,并将该副本附加到列表中。”“不正确。

      在第一种情况下,您引用的是同一个对象,因此您基本上是在列表中一次又一次地推同一个对象,而在第二种情况下,您每次都推一个新对象。谢谢。您的回答很有意义。因此,每次我这样做时,list.add(map)。我在列表中仍然有原始地图的参考。这意味着,如果我在将地图添加到列表后对其进行外部更改,列表中的数据也将正确更改?是否有方法在列表中实际存储原始数据的副本,并断开原始地图与列表之间的链接?谢谢
      [{M: m 0, D: d : 0}, {M: m 1, D: d : 1}, {M: m 2, D: d : 2}, {M: m 3, D: d : 3}, {M: m 4, D: d : 4}]
      
      for (int i = 0; i < 5; i++) {
          print('\n $i => ${_tempMap} \n');