如果在波动中listA = listB,如何防止元素不从listB中删除和从listA中删除?

时间:2020-08-25 15:43:43

标签: android list flutter dart remove

在flutter中,我有两个列表,代码如下。 我从ListA中删除了一个元素,但也从ListB中删除了它,为什么会发生??

List<String> listA,listB=["b1","b2"];
      listA=listB;
      print("listA:"+listA.toString()+"   listB:"+listB.toString());
      listA.remove("b1");
      print("listA:"+listA.toString()+"   listB:"+listB.toString());

这给出的输出为

2020-08-25 21:04:49.788 11243-11552/app.example I/flutter: listA:[b1, b2]   listB:[b1, b2]
2020-08-25 21:04:49.788 11243-11552/app.example I/flutter: listA:[b2]   listB:[b2]

如何仅从ListA而不是ListB删除元素?

2 个答案:

答案 0 :(得分:0)

要单独编辑2个列表,您需要2个列表。

的输出
main() {
  List<String> listA,listB=["b1","b2"];
  listA=listB.toList();
  print("listA:"+listA.toString()+"   listB:"+listB.toString());
  listA.remove("b1");
  print("listA:"+listA.toString()+"   listB:"+listB.toString());
}

listA:[b1, b2]   listB:[b1, b2]
listA:[b2]   listB:[b1, b2]

答案 1 :(得分:0)

您没有两个列表开头。您只有一个列表。您有两个引用该列表的变量。

如果要有两个列表,则需要创建一个 new 。所以这个:

getProcessPercentCPU("Google Chrome")

on getProcessPercentCPU(someProcess)
    
    set i to 0
    set test to 0
    
    repeat while test < 50
        set test to ¬
            (do shell script "ps -xco %cpu,command | awk '/" & someProcess & "$/ {print $1}'") ¬
                as integer
        delay 2
        set i to i + 1
        if i ≥ 10 then exit repeat      
    end repeat
    
    display dialog test
    
end getProcessPercentCPU

需要成为

listA=listB;

或者因为您可以省略listA = new List.from(listB); 关键字:

new

那么您实际上有两个列表,从一个列表中删除不会修改另一个列表。

相关问题