先比较,再用remove方法吧,
public class LinkedListTest {
/**
* @param args
*/
public static void main(String[] args) {
LinkedList list = new LinkedList();
String[] str = {"K" ,"DD", "W", "C", "DD", "N", "Q","C"};
for(int i=0;i<str.length;i++){
list.add(str[i]);
}
String last;
String next;
for(int i=0;i<list.size()-1;i++){
for(int j=i+1;j<list.size();j++){
last = (String)list.get(i);
next = (String)list.get(j);
if(last.equals(next)){
list.remove(j);
}
}
}
System.out.println(list);
}
} |