Collections sort not being accepted by Eclipse

半腔热情 提交于 2019-12-01 19:36:26
alyu

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.

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:

  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());

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!