Eclipse不接受集合排序 [英] Collections sort not being accepted by Eclipse

查看:116
本文介绍了Eclipse不接受集合排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    import java.io.BufferedReader;
    import java.util.Collections;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.Date;
    import java.util.List;
    import com.javaranch.common.TextFileIn;

    public class SortNames 
    {
        public static class CelebrityNamesFile
        {
            public String firstName;
            public String lastName;

            public static class CompareLastName implements Comparator< CelebrityNamesFile >
            {
                @Override
                public int compare( CelebrityNamesFile o1,  CelebrityNamesFile o2 )
                {
                    return o2.lastName.compareTo( o1.lastName );
                }
            }

        public static void main( String[ ] args ) throws IOException
        {

            ArrayList< CelebrityNamesFile > myCelebrityList;
            myCelebrityList = new ArrayList< CelebrityNamesFile >();

            TextFileIn celebrityNamesFile = new TextFileIn( "celebrityNamesFile.txt" );
            boolean doneReadingCelebrityNames = false;
            while ( ! doneReadingCelebrityNames )
            {
                 String oneName = celebrityNamesFile.readLine();
                 if ( oneName == null )
                 {
                     doneReadingCelebrityNames = true;
                 }

$
Eclipse不喜欢后面的add语句,即:类型ArrayList(SortNames.CelebrityNamesFile)中的方法add(SortNames.CelebrityNamesFile)不适用于参数(String)

                 else
                 {
                     myCelebrityList.add( oneName );
                 }
            }
            celebrityNamesFile.close();

$
然后它不喜欢我的排序声明,即:
绑定不匹配:类型集合的泛型方法排序(列表T)不适用于参数(ArrayList(SortNames.CelebrityNamesFile))。推断类型SortNames.CelebrityNamesFile不是有界参数的有效替代(T extends Comparable(?super T))

            Collections.sort( myCelebrityList );
            System.out.println( myCelebrityList );

            Collections.sort( myCelebrityList, new CelebrityNamesFile.CompareLastName() );
            System.out.println( myCelebrityList );
}
}

}

我做错了什么?我在这里阅读了很多帖子,阅读了有关比较器的Java文档,阅读了有关比较器的Java教程。 Head First Java,Ivor Horton开始使用Java 7.仍然无能为力。

What am I doing wrong? I have read many posts here, have read the Java docs regarding comparator, have read Java tutorials regarding comparator. Head First Java, Ivor Horton's beginning Java 7. Still clueless.

该程序的目的是从txt文件中读取名称,将它们添加到arraylist,打印按自然顺序排列的arraylist,按姓氏排序arraylist,再次打印列表。

The purpose of the program is to read names from a txt file, add them to an arraylist, print the arraylist in its natural order, sort the arraylist by last name, print the list again.

推荐答案

对于第一个问题,问题很简单。 oneName 字符串,而 myCelebrityList 是一个集合 CelebrityNamesFile - 这意味着您只能将对象添加到 CelebrityNamesFile 类型的集合中。

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.

对于第二个问题,您的问题是 CelebrityNamesFile 类没有实现 Comparable 界面。排序要求对列表元素类型实施排序,例如,通过为该类实现 Comparable 或向 sort Comparator c>。

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.

这篇关于Eclipse不接受集合排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆