刚学到Comparable 接口 , 出现了点小问题 求指点
程序代码:
import java.util.*; public class TestComparable{ public static void main(String args[]){ List l = new ArrayList(); l.add(new testname("Giggs","wong") ); l.add(new testname("beckham","li") ); l.add(new testname("jack","chen") ); l.add(new testname("suki","xu") ); Collections.reverse(l); System.out.println(l); Collections.sort(l); // 排序testname 类 // 怎么老出现异常 System.out.println(l); } } class testname{ public String firstname; public String lastname; testname(String f,String l){ firstname = f; lastname = l; } public int compareTo(Object o){ testname t = (testname) o; int bo = (t.lastname); return ( bo !=0 ? bo : (t.firstname) ); } public String toString(){ return lastname+" "+firstname; } }