2015-10-26 89 views
0

我有以下行错误:Java错误日食排序

Collections.sort(l); 

你能解释一下为什么吗?

这是程序代码:

import java.io.*; 
import java.util.*; 

public class Esame{ 

    public static void main(String args[]) throws IOException{ 
     ArrayList<Studente> l=new ArrayList<Studente>(); 
     BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); 
     String cognome = b.readLine(); 
     while(!cognome.equals("")){ 
      System.out.println("inserire nome"); 
      String nome = b.readLine(); 
      System.out.println("inserire matricola"); 
      String matricola = b.readLine(); 
      Studente s = new Studente(nome, cognome, matricola); 
      l.add(s); 
      System.out.println("inserire cognome"); 
      cognome = b.readLine(); 
     } 
     b.close(); 
     Collections.sort(l); 
     Iterator<Studente> i=l.iterator(); 
     PrintStream p=new PrintStream(new File("uscita.txt")); 
     while(i.hasNext()){ 
      p.println(i.hasNext()); 
     } 
     p.close(); 
    } 

} 

class Studente{ 

    protected String nome,cognome; 
    protected int matricola; 

    public Studente(String nome,String cognome,String matricola){ 
     this.nome=nome; 
     this.cognome=cognome; 
     this.matricola=Integer.parseInt(matricola); 
    } 

    public String toString(){ 
     return "\n NOME: "+nome+"Cognome: "+cognome+"MATRICOLA: "+matricola; 
    } 

    public int compareTo(Object x) { 
     Studente p = (Studente) x; 
     return cognome.compareTo(p.cognome); 
    } 
} 
+1

打印控制台输出,所以我们可以读取异常请。 –

+0

什么是错误?是否堆栈跟踪,然后请张贴,或者如果它的日食错误比张贴屏幕截图 –

+0

我有@override尝试,但是,不会改变任何东西:错误是这样的:异常在线程“主要”java.lang.Error:未解决编译问题: \t的方法排序(名单)在类型集合不适用于参数(ArrayList的) \t在Esame.main(Esame.java:31) –

回答

1

类STUDENTE必须实现Comparable接口,以便进行排序!

否则.sort(...)不知道如何比较Studente对象。

1

只要将您的Arraylist更改为List即可。

ArrayList<Studente> l=new ArrayList<Studente>();

应该

List<Studente> l=new ArrayList<Studente>();

Collections.sort method accepts an列表and not an ArrayList`

从规格

api

片段

Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).

Studente类应该实现Comparable界面为你sort正常工作。

ImplementComparable接口在Studente

class Studente implements Comparable<Studente>{ 

    protected String nome,cognome; 
    protected int matricola; 

    public Studente(String nome,String cognome,String matricola){ 
     this.nome=nome; 
     this.cognome=cognome; 
     this.matricola=Integer.parseInt(matricola); 
    } 

    public String toString(){ 
     return "\n NOME: "+nome+"Cognome: "+cognome+"MATRICOLA: "+matricola; 
    } 

    @Override 
    public int compareTo(Studente o) { 
     // Logic for compare has to go here 
     return 0; 
    } 
+0

这只会解决问题的一部分;) –

+0

@Parker_Halo改变'List'和执行'比较'应该可以解决他的问题。 –

+0

@ShiladittyaChakraborty如果OP正确地实现了“可比”接口,则边界不匹配不会发生。 –

2

变化

1)在学生类中实现Comparable接口

public class Studente implements Comparable<Studente>{ 
} 

2.)更改重写方法compareTo

@Override 
    public int compareTo(Studente o) { 
     Studente p = (Studente) o; 
     return cognome.compareTo(p.cognome); 
    } 

从Javadoc中,

Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).

参考Collections#sort更多细节

2
class Studente implements Comparable&lt;Studente&gt;{ 

    protected String nome,cognome; 
    protected int matricola; 

    public Studente(String nome,String cognome,String matricola){ 
     this.nome=nome; 
     this.cognome=cognome; 
     this.matricola=Integer.parseInt(matricola); 
    } 

    public String toString(){ 
     return "\n NOME: "+nome+"Cognome: "+cognome+"MATRICOLA: > "+matricola; 
    } 

    public int compareTo(Studente x) { 
     //how to compare 
     return 0; 
    } 

你必须让你的类实现可比。

+1

您必须插入逻辑来比较并根据比较结果返回0,1,-1。 –

3

您只需implements Comparable<T>来解决这个问题,或者你可以传递一个Comparatorsort方法,如:

Collections.sort(l, new Comparator<Studente>() { 
      @Override 
      public int compare(Studente o1, Studente o2) { 
       // some kind of comparation 
       return o1.matricola - o2.matricola; 
      } 
     }); 

这样你的类并不需要实现Comparable接口