例如,如果與ArrayList
set1 = {"a", "a", "b", "a", "b", "c", "d", "e"}
,ArrayList
與set2 = {"a", "b"}
。然後除去a
發生和b
set1
將具有{"c", "d", "e"}
刪除相繼出現的元素數組列表
例如,如果用的ArrayList SET1 = { 「一」, 「一」,「 b」, 「一」, 「b」, 「C」, 「d」, 「e」 的} 的ArrayList與SET2 = { 「一」, 「b」}
then remove occurence of a and b set1 will have {"c", "d", "e"}
我曾嘗試:
public class test {
public static void main(String[] args){
ArrayList<String> contents = new ArrayList<String>();
contents.add("a");
contents.add("a");
contents.add("b");
contents.add("b");
contents.add("c");
contents.add("d");
contents.add("e");
ArrayList<String> delete = new ArrayList<String>();
delete.add("a");
delete.add("b");
for(String str: contents)
System.out.println(str);
System.out.println();
contains(contents, delete);
System.out.println();
for(String st: contents)
System.out.println(st);
}
public static void contains(ArrayList<String> set1, ArrayList<String> set2) {
int count = 0;
OUTER:
for (int i = 0; i < set1.size() - set2.size(); i++) {
for (int j = 0; j < set2.size(); j++) {
if (!set1.get(i + j).equals(set2.get(j)))
continue OUTER;
else{
set1.remove(set1.get(i+j));
}
}
count++;
}
System.out.println(count);
}
電流輸出:一個 b Ç d Ë 我仍然得到A B C dé但是我只應該應該都會得到C dË因爲自A,B中出現兩次,應刪除這兩個出現次數。
聽起來像功課。沒關係,但你必須展示你到目前爲止所嘗試的。 – Parker 2014-10-22 03:50:32
實際上發佈你的代碼甚至不像提問一樣重要。 – Radiodef 2014-10-22 03:55:48
@NovPi好的問題開始你的學習,請嘗試,接受挑戰並自己發佈答案。你會感到自豪 – spiderman 2014-10-22 03:57:39