class Word{
String word;
Word({required this.word});
}
List<Word> firstList = ["Add","All","Human","Moon","Free"];
List<Word> secondList= ["Add","Human","Free"];
如果第一個串列中的元素存在于第二個串列中,我希望它回傳 true,否則回傳 false。
輸出應該是:
true
false
true
false
true
uj5u.com熱心網友回復:
首先,我會確保Word
可以輕松比較:
class Word {
String word;
Word({required this.word});
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is Word && other.word == word;
}
@override
int get hashCode => word.hashCode;
}
然后你可以這樣做:
print(firstList.map((w) => secondList.contains(w)));
檢查這個DartPad
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/491948.html