Collections sort not being accepted by Eclipse

后端 未结 5 1591
梦如初夏
梦如初夏 2021-01-19 06:58
    import java.io.BufferedReader;
    import java.util.Collections;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader         


        
相关标签:
5条回答
  • 2021-01-19 07:07

    The collection myCelebrityList is of type CelebrityNamesFile and that means it can only accept instances of that class. You need to create an instance of CelebrityNamesFile and add it to the collection. The Collections wont accept your collection because you class CelebrityNamesFile does not implement the Comparator interface. You need to pass your comparator instance to the Collections.sort() as second parameter.

    0 讨论(0)
  • 2021-01-19 07:12

    The Comparator implemented in the CompareLastName class can be put outside the SortNames class, or at least outside the CelebrityNamesFile class. I was having the exact same issue until I put the class that implements my Comparator outside of my object class. After I did this, the program worked perfectly. Here's a sample of my code with comments if it helps.

    // Comparator interface for my Word Class.
    public interface Comparator<Word>
    { 
        int compare(Word first, Word second); 
    }
    
    // Word Class, which is my object.
    public class Word
    {
        private String word;
        public Word(String input)  { word = input; }
        public String get()  { return word; }
        public int wordSize()  { return word.length(); }
    }
    
    // Comparator implementation is separate from my Word Class.
    public class WordComparator implements Comparator<Word>
    {
        public int compare(Word first, Word second)
        {
            if (first.wordSize() < second.wordSize())  { return -1; }
            else if (first.wordSize() > second.wordSize())  { return 1; }
            else  { return first.get().compareTo(second.get()); }
        }
    }
    
    // Now run the program to ensure the Word Class objects and the WordComparator
    // Class are implemented correctly.
    public class WordComparatorDemo
    {
        public static void main(String[] args) throws FileNotFoundException
        {
            ArrayList<Word> list = new ArrayList<Word>();
            JFileChooser find = new JFileChooser();
            Scanner read = null;
            if(find.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
            {
                File selectedFile = find.getSelectedFile();
                read = new Scanner(selectedFile);
                try
                {
                    list = inputData(read);
                    // Here's the sort implementing the WordComparator.
                    Collections.sort(list, new WordComparator());
                }
                finally  { read.close(); }
            }
        }
        public static ArrayList<Word> inputData(Scanner in)
        {
            ArrayList<Word> list = new ArrayList<Word>();
            in.useDelimiter("[^A-Za-z]+");
            while(in.hasNext())
            {
                String word = in.next();
                Word temp = new Word(word);
                list.add(temp);
            }
            return list;
        }
    }
    

    Note: My answer is a year after the original post, but hopefully it will help anyone who visits this site for help.

    0 讨论(0)
  • 2021-01-19 07:16

    For the first issue, the problem is fairly simple. oneName is a String, and myCelebrityList is a collection of CelebrityNamesFile- which means that you can only add objects to the collection of type CelebrityNamesFile.

    For the second issue, your problem is that the CelebrityNamesFile class does not implement the Comparable interface. Sorting requires that an ordering is implemented for the list element type, e.g. by implementing Comparable for the class or providing a Comparator to the sort.

    0 讨论(0)
  • 2021-01-19 07:17
    1. You're trying to add a String (the object oneName) to a list of CelebrityNamesFiles. You need to somehow create a CelebrityNamesFile out of this String.
    2. Collections.sort() only works on objects that are Comparable if you don't explicitly pass a comparator. Try this instead:

      Collections.sort(myCelebrityList, new CelebrityNamesFile.CompareLastName());

    0 讨论(0)
  • 2021-01-19 07:19

    For first issue, you're trying to add a String into a List<CelebrityNamesFile> instance. You sould use the String to create a CelebrityNamesFile instance and add this instance to your list:

    CelebrityNamesFile celebrityNamesFile = new CelebrityNamesFile();
    celebrityNamesFile.lastName = oneName;
    myCelebrityList.add( celebrityNamesFile );
    

    For second issue, try

    Collections.sort(myCelebrityList, new CompareLastName());
    

    As @alyu points, the CelebrityNamesFile class needs to implement the Comparable interface to work with the code you're posting, but you could also add a Comparator<CelebrityNamesFile> instance (and you already have the CompareLastName class that implements it).

    More about this:

    • Collections#sort
    0 讨论(0)
提交回复
热议问题