谢谢马尔科。我重写代码。尽量让它简单。这次真的可以编译了。但它只能删除相邻的重复项目。例如,如果我输入 1 2 3 3 4 4 5 1 - 输出为 1 2 3 4 5 1。它不能在最后提取副本。(顺便说一句:这个网站的新手,如果让任何显示混乱我很抱歉)
这是新代码:
import java.util.*;
public class SetListDemo{
public static void main(String[] args){
SetListType newList = new SetListType();
Scanner keyboard = new Scanner(System.in);
System.out.println( "Enter a series of items: ");
String input = keyboard.nextLine();
String[] original = input.split(" ");
for (String s : original)
newList.insert(s);
List<String> finalList = new ArrayList(Arrays.asList(original)) ;
Iterator<String> setIterator = finalList.iterator();
String position = null;
while(setIterator.hasNext()){
String secondItem = setIterator.next();
if(secondItem.equals(position)){
setIterator.remove();
}
position = secondItem;
}
System.out.println("\nHere is the set list:");
displayList(finalList);
System.out.println("\n");
}
public static void displayList(List list){
for(int index = 0; index <list.size(); index++)
System.out.print(list.get(index) + ", ");
}
}