Collections sort not being accepted by Eclipse

后端 未结 5 1672
梦如初夏
梦如初夏 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:19

    For first issue, you're trying to add a String into a List 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 instance (and you already have the CompareLastName class that implements it).

    More about this:

    • Collections#sort

提交回复
热议问题