已知字串a="1,2,3,4,5,6,7,8"
已知字串b="1,3,4"
獲取他們不相同的部分
當後端需要返回給前端完整的任務列表, 任務列表一共有八個, 已知完成了三個任務
(從資料庫中獲取), 如何取得未完成的任務列表)
方法一:方法一:
把總任務列表和已完成的任務列表, 放入list的集合中,
遍歷總完成的任務列表, 依次跟已完成任務列表比較,
如果總任務列表的字串不在已完成任務列表, 則放入未完成列表中.
方法二:
把總任務列表和已完成的任務列表, 放入list的集合中,
遍歷總完成的任務列表, 使用contains()方法判斷已完成列表的字串是否包含總任務列表中的字串,
把不包含的放在未完成列表中.
方法三:
把總任務列表和已完成的任務列表, 放入list的集合中,
兩個list去重最簡單的方法, list1.removeall(list2)
final string taskliststring = "1,2,3,4,5,6,7,8";
string taskarray = taskliststring.split(",");
listlist = arrays.aslist(taskarray);
listfinishedstr = new arraylist();
finishedstr.add("1");
finishedstr.add("3");
finishedstr.add("4");
listunfinishedstr = new arraylist();
for (int i = 0; i < list.size(); i++)
}if(flag == false)
}system.out.println(unfinishedstr);
注意點: 在字串\集合比較的時候, 只能equals 方法, 用==是無法判斷的.
方法二:
final string taskliststring1 = "1,2,3,4,5,6,7,8";
string taskarray = taskliststring1.split(",");
listactionstr = arrays.aslist(taskarray);
final string taskliststring2 = "1,3,4";
string finishedarray = taskliststring2.split(",");
listfinishedstr= arrays.aslist(finishedarray);
listunfinishedstr = new arraylist();
for (string taskstr : actionstr)
}system.out.println(unfinishedstr);
注意: 使用contain方法時, 字串b必須是字串a的子集.
如果字串b不在a的範圍內,也不會顯示
方法三:
final string taskliststring = "1,2,3,4,5,6,7,8";
string taskarray = taskliststring.split(",");
listlist = arrays.aslist(taskarray);
listfinishedstr = new arraylist();
finishedstr.add("1");
finishedstr.add("3");
finishedstr.add("4");
listnewlist = new arraylist<>(list);
newlist.removeall(finishedstr);
system.out.println(newlist);
注意: 使用removeall()方法時, 字串b必須是字串a的子集.
否則可能跟你想要的結果有所偏差
python 合併兩個列表並去重
合併兩個列表並去重 10分 題目內容 輸入兩個列表alist和blist,要求列表中的每個元素都為正整數且不超過10 合併alist和blist,並將重複的元素去掉後輸出乙個新的列表clist。可以使用以下實現列表alist的輸入 alist list map int,input split 同時為...
python入門 兩個列表合併去重
輸入兩個列表alist和blist,要求列表中的每個元素都為正整數且不超過10 合併alist和blist,並將重複的元素去掉後輸出乙個新的列表clist。可以使用以下實現列表alist的輸入 alist list map int,input split 同時為保證輸出結果一致,請將集合內元素排序之...
1合併兩個列表並去重
題目內容 輸入兩個列表alist和blist,要求列表中的每個元素都為正整數且不超過10 合併alist和blist,並將重複的元素去掉後輸出乙個新的列表clist。可以使用以下實現列表alist的輸入 alist list map int,input split 同時為保證輸出結果一致,請將集合內...