java需要关于从数组列表中删除重复项的代码的帮助
到目前为止,我已经制作了一个数组,将文本输入记事本,这样可以保存文件并进行字数计算。我也制作了一个数组,这样就可以按字母顺序排列。但是,我正在努力从文件中删除重复的字,例如“我是凯文,我很了不起” 我想这个代码来计算的话,排序成字母顺序,并删除重复的话 前两个都完成了,第三个我还在努力java需要关于从数组列表中删除重复项的代码的帮助,java,Java,到目前为止,我已经制作了一个数组,将文本输入记事本,这样可以保存文件并进行字数计算。我也制作了一个数组,这样就可以按字母顺序排列。但是,我正在努力从文件中删除重复的字,例如“我是凯文,我很了不起” 我想这个代码来计算的话,排序成字母顺序,并删除重复的话 前两个都完成了,第三个我还在努力 public class Program3 { public static void main(String[] args) { ArrayList<order>
public class Program3 {
public static void main(String[] args) {
ArrayList<order> ordering = new ArrayList<order>();
String words = readTextFile(filename);
int lineEnd = words.length() - 1;
boolean isWord = false;
String tempWord = "";
String temp = "";
for(int x = 0; x<words.length(); x++){
if(Character.isLetter(words.charAt(x)) && x != lineEnd){
isWord=true;
tempWord = tempWord + words.charAt(x);
}else if (!Character.isLetter(words.charAt(x)) && isWord){
isWord = false;
ordering.add(new order(tempWord));
tempWord="";
}else if(Character.isLetter(words.charAt(x)) && x == lineEnd){
tempWord = tempWord + words.charAt(x);
}
}
for (order o : ordering){
o.display();
}
System.out.println("");
Comparator c = new Comparator<order>(){
@Override
public int compare(order word1, order word2)
{
return word1.ordering().compareTo(word2.ordering());
}
};
ordering.sort(c);
for (order o : ordering){
o.display();
}
//remove duplicates
}
public static String readTextFile(String filename){
String returnValue="";
FileReader file = null;
try {
file = new FileReader(filename);
BufferedReader reader = new BufferedReader(file);
String line = "";
while ((line = reader.readLine()) != null){
returnValue = returnValue + line + "\n";
}
reader.close();
} catch(Exception e){
throw new RuntimeException(e);
}finally{
if (file!=null){
try {
file.close();
} catch (IOException e){
//Ignore issues during closing
}
}
}
return returnValue;
}
}
公共课程3{
公共静态void main(字符串[]args){
ArrayList排序=新建ArrayList();
字符串字=readTextFile(文件名);
int lineEnd=words.length()-1;
布尔值isWord=false;
字符串tempWord=“”;
字符串temp=“”;
对于(int x=0;XY您可以使用Set来删除重复项,而不是列表。请使用Set…并确保在尝试使用Set
之前实现了order.hashcode
和order.equals
。但是,在使用Set时,您需要在读取双字时对其进行计数。供将来参考:此这个问题和日食无关,所以给它加上这样的标签可能会导致人们拒绝回答