Exception in thread "main" java.lang.ClassCastException: minc.JSGTE.util.mobile.MobileInfo
at java.util.TreeMap.compare(Unknown Source)
at java.util.TreeMap.getEntry(Unknown Source)
at java.util.TreeMap.containsKey(Unknown Source)
at java.util.TreeSet.contains(Unknown Source)
at minc.JSGTE.util.mobile.MobileCollection.add(MobileCollection.java:46)
at minc.JSGTE.util.mobile.MobileCollection.<init>(MobileCollection.java:37)
at minc.JSGTE.util.mobile.MobileCollection.main(MobileCollection.java:16)MobileInfo是我做的一个类,是不是要实现什么借口或者重载什么方法?谢谢指教!

解决方案 »

  1.   

    代码比较多,发出来不大好看,呵呵!package minc.JSGTE.util.mobile;
    import java.util.Comparator;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class MobileInfo implements Comparator{
    public static int INFO_TYPE_NUMBER = 0;
    public static int INFO_TYPE_USER = 1;
    public static int INFO_TYPE_GROUP = 2;

    private static int idNum = 0; int id  = idNum++;
    int type = 0;    //           0:号码;           1:名称;      2:群组
    String caption = "";//显示内容  13346666666            张三          朋友组
    String value = "";  //值        13346666666         13346666666       2
    int qty = 0; public static void main(String[] args) {
    MobileInfo m = new MobileInfo("asdf<1334983387345565422101>");
    System.out.println(m.getCaption());
    System.out.print(m.getValue());
    }

    public MobileInfo(String info)
    {
    if(info == null || info == "")
    return;

    if(MobileUtil.isNumber(info))
    {
    this.type = INFO_TYPE_NUMBER;
    this.qty  = 1;
    this.caption = info;
    this.value   = info;
    return;
    }
    String re1 = "([^\\x00]*)\\[([\\d]+)\\]"; 
            Pattern p = Pattern.compile(re1);  //匹配 A#1 B2 B#44
            Matcher m = p.matcher(info);
    if(m.find())
    {
    this.type = INFO_TYPE_GROUP;
    this.qty  = 0;
    this.caption = m.group(1);
    this.value   = m.group(2);
    return;
    }
    String re2 = "([^\\x00]*)<([\\d]+)>"; 
            Pattern p2 = Pattern.compile(re2);  //匹配 A#1 B2 B#44
            Matcher m2 = p2.matcher(info);
    if(m2.find())
    {
    this.type = INFO_TYPE_USER;
    this.qty  = 1;
    this.caption = m2.group(1);
    this.value   = m2.group(2);
    return;
    }
    }
    public String getCaption() {
    return caption;
    } public void setCaption(String caption) {
    this.caption = caption;
    } public int getId() {
    return id;
    } public int getQty() {
    if(this.type == INFO_TYPE_GROUP && this.qty == 0)
    {
    int groupId = Integer.parseInt(this.value);
    this.qty = getQtyByGroup(groupId);
    }
    return qty;
    }
    /**
     * 获取群的数量,操作数据库 TODO:操作数据库
     * @param groupId
     * @return
     */
    private int getQtyByGroup(int groupId)
    {
    //TODO:
    return 100;
    }
    /**
     * 获取群组的手机列表,操作数据库 TODO:操作数据库
     * @param groupId
     * @return
     */
    private String[] getMobileListByGroup(int groupId)
    {
    //TODO:
    return new String[]{"1111111","222222","33333333"};
    }

    /**
     * 获取手机列表,如果是群组类型,操作数据库 TODO:操作数据库
     * @return
     */
    public String[] getMobileList()
    {
    if(this.type == INFO_TYPE_GROUP)
    {
    int groupId = Integer.parseInt(this.value);
    return getMobileListByGroup(groupId);
    }
    return new String[]{this.value};
    } public void setQty(int qty) {
    this.qty = qty;
    } public int getType() {
    return type;
    } public void setType(int type) {
    this.type = type;
    } public String getValue() {
    return value;
    } public void setValue(String value) {
    this.value = value;
    } @Override
    public int hashCode() {
    return super.hashCode();
    } @Override
    public boolean equals(Object obj) {
    // TODO Auto-generated method stub
    if(obj != null && obj instanceof MobileInfo)
    {
    MobileInfo mInfo = (MobileInfo)obj;
    if( mInfo.getCaption().equals(this.caption) &&
    mInfo.getType() == this.type &&
    mInfo.getValue().equals(this.value))
    return true;
    else
    return false;
    }
    else
    return false;
    } public int compare(Object arg0, Object arg1) {

    MobileInfo mInfo1 = (MobileInfo)arg0;
    MobileInfo mInfo2 = (MobileInfo)arg1;

    return (arg0==null? arg1==null : arg0.equals(arg1))?1:0; 
    }
    }
      

  2.   

    public int compare(Object arg0, Object arg1) {

    MobileInfo mInfo1 = (MobileInfo)arg0;
    MobileInfo mInfo2 = (MobileInfo)arg1;
    }应该是这里的转型错误了
    加上
    if(obj != null && obj instanceof MobileInfo)
    好一点