Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java需要关于从数组列表中删除重复项的代码的帮助_Java - Fatal编程技术网

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时,您需要在读取双字时对其进行计数。供将来参考:此这个问题和日食无关,所以给它加上这样的标签可能会导致人们拒绝回答