我想从输入文件中删除重复的名称并打印出所有名称只有一次。这里是我的代码:错误查找和删除java中的重复项
public static void main(String[] args) throws IOException {
ArrayList<String> fn = new ArrayList<String>();
ArrayList<String> ln = new ArrayList<String>();
ArrayList<String> names = new ArrayList<String>();
getNames(fn,ln);
System.out.println("\n******* All Unique Names*********");
remove(names);
}
public static int find(String s, ArrayList<String> a) {
for (int i = 0; i < a.size(); i++)
if (a.get(i).equals(s))
return i;
return -1;
}
public static int remove(ArrayList<String>n){
//int found = find(names, n);
int index = 0;
while (index < n.size() - 1) {
if (n.get(index).equals(n.get(index + 1))) {
n.remove(index + 1);
} else {
index++;
}
}
System.out.println(n);
return index;
}
}
如何打印没有重复的名称?
不'ArrayList'有'contains'方法? – Arc676