import java.util.*;
class ArrayListTest
{
static void printElements(Collection cl)
{
Iterator it=cl.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}

}
public static void main(String[] args)
{
ArrayList<Object> al=new ArrayList<Object>();
al.add(new Students(1,"zhangsan"));
al.add(new Students(2,"lisi"));
al.add(new Students(3,"wangwu"));
Collections.sort(al);//为什么这个地方出错呢?
printElements(al);
}
}
class Students implements Comparable
{
int num;
String name;
Students(int num,String name)
{
this.num=num;
this.name=name;
}
public int compareTo(Object o)
{
Students st=(Students)o;
int result=num>st.num?1:(num==st.num?0:-1);
return result;
}
public String toString()
{
return "num="+num+"name="+name;
}
}
为什么提示找不到符号

解决方案 »

  1.   

    ArrayList<Object> al=new ArrayList<Object>();
    为什么不直接用<Students>?
      

  2.   

    如果把Object换成students
    他就回提示使用了未经检查或不安全的操作但是可以Compile通过
    但是使用Object 不能编译
      

  3.   

    ArrayList<Students> al=new ArrayList<Students>();
    class Students implements Comparable<Students>
    public int compareTo(Students o)
    把以上三个地方改成这样
    Students st=(Students)o;//把这个强制转换可以去掉了
      

  4.   

    可能是因为Object 没有compareTo方法把
      

  5.   

    ArrayList<Object> al=new ArrayList<Object>();改为:
    List al = (ArrayList)new ArrayList();num = 1,name: zhangsan
    num = 2,name: lisi
    num = 3,name: wangwu
      

  6.   

    ArrayList<Object> al=new ArrayList<Object>();
    ........
    Collections.sort(al);//为什么这个地方出错呢?
    --------------------------------------------------
    Object不是Comparable型的,所以会抱错.
      

  7.   

    package net.oicp.sunflowerbbs;import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    class PrimaryKey implements Comparable<PrimaryKey>
     {
    int key1;
    int key2;
    PrimaryKey(int key1, int key2) {
    this.key1 = key1;
    this.key2 = key2;
    }

    public int compareTo(PrimaryKey another)
    {
        if(this.key1<=another.key1) 
         return 0;
        else if(this.key1>another.key1)
         return 1;
        else if (this.key2<=another.key2)
         return 0;
        else
         return 1;
    } public String toString() {
    return "(" + this.key1 + "," + this.key2 + ")";
    }
    }class Comp implements Comparator<PrimaryKey> {
    public int compare(PrimaryKey o1, PrimaryKey o2) {
    if (o1.key1 < o2.key1)
    return 0;
    else if (o1.key1 > o2.key1)
    return 1;
    else if (o1.key2 <= o2.key2)
    return 0;
    else
    return 1;
    }
    }public class Sort {
    public static void main(String[] args) {
    ArrayList<PrimaryKey> list = new ArrayList<PrimaryKey>();
    list.add(new PrimaryKey(1, 111));
    list.add(new PrimaryKey(1, 112));
    list.add(new PrimaryKey(3, 333));
    list.add(new PrimaryKey(3, 339));
    list.add(new PrimaryKey(2, 222));
    Collections.<PrimaryKey>sort(list);
    for (int i = 0; i < list.size(); i++) {
    PrimaryKey p = (PrimaryKey) list.get(i);
    System.out.println(p);
    }
    }
    }
      

  8.   

    上面的Comp类多余,是另一种sort方法.