首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

LinkedList排序有关问题,大家来帮忙呀!

2012-03-06 
LinkedList排序问题,大家来帮忙呀!!!!!!!!!!!!!!!!!代码如下:importjava.util.LinkedListimportjava.util

LinkedList排序问题,大家来帮忙呀!!!!!!!!!!!!!!!!!
代码如下:
import   java.util.LinkedList;
import   java.util.Collections;
import   java.util.List;
public   class   myLinked
{
    LinkedList   llObj;
    myLinked()
    {
        llObj   =   new   LinkedList();
    }
    void   input(Object   obj)
    {
        llObj.addLast(obj);
    }
    void   output()
    {
        for(int   i=0;i <llObj.size();i++)
        {
            System.out.println( "姓名: "+((Info2)(llObj.get(i))).getName());
            System.out.println( "年龄: "+((Info2)(llObj.get(i))).getAge());
            System.out.println( "国家: "+((Info2)(llObj.get(i))).getCountry());
            System.out.println();
        }
    }
    void   sort()
    {
        Collections.sort(llObj);
        this.output();
    }
    public   static   void   main(String[]   args)
    {
        myLinked   my   =   new   myLinked();
        my.input(new   Info2( "Aaron ",21, "CHINA   PR "));
        my.input(new   Info2( "POPO ",19, "CHINA   PR "));
        my.input(new   Info2( "Milk ",21, "USA "));
        my.sort();
    }
}

class   Info2
{
    private   String   name;
    private   int   age;
    private   String   country;

    Info2(String   name,int   age,String   country)
    {
        this.name   =   name;
        this.age   =   age;
        this.country   =   country;
    }

    public   String   getName()
    {
        return   this.name;
    }

    public   int   getAge()
    {
        return   this.age;
    }

    public   String   getCountry()
    {
        return   this.country;
    }
}

问题是这样的:     我把new   的实例存入LinkedList后,想通过Collections类的sort方法排序后输出,但是系统总会报错.不能进行排序,到底是什么原因呢??   我想了很久都无法解决,   是不是不能对链表中的实例排序呢?高手快来帮忙呀,还有就是   我输出LinkedList中的内容可以用   for循环输出吗?就象上面的代码一样,还是必须用迭代Iterator呢?????       谢谢大家了,   一鞠躬,二鞠躬!!!!!!!!!!!!!!!!!!!!!!

[解决办法]
Info2实现Comparable接口
然后实现compareTo方法.

如果按名字比较就加上
public int compareTo(Object o) {
return name.compareTo(((Info2) o).getName());
}

如果按年龄比较就加上
public int compareTo(Object o) {
return age - ((Info2)o).getAge();
}
或者什么其他的方法比较.
------解决方案--------------------


用Collections类的sort排序方法必须要实现Comparable接口才可以.
[解决办法]
LinkedList 排序在List中是比较差的,相对于ArrayList 和 Vector而言

主要是 get(int i)
public Object get(int index) {
return entry(index).element;
}

private Entry entry(int index) {
if (index < 0 || index > = size)
throw new IndexOutOfBoundsException( "Index: "+index+
", Size: "+size);
Entry e = header;
if (index < (size > > 1)) {
for (int i = 0; i <= index; i++)
e = e.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
}
return e;
}

光顾移动指针了
[解决办法]
Collections.sort(llObj);

->

Collections.sort(llObj, new Comparator(){
public int compare(Object o1, Object o2) {
Info2 i1 = (Info2)o1;
Info2 i2 = (Info2)o2;
// .... some compare operations to get the result here
return 0;
}

});
[解决办法]
Collections.sort(list, new Comparator <Info2> () {
public int compare(Info2 o1, Info2 o2) {
if (!o1.getName().equals(o2.getName())) {
return o1.getName().compareTo(o1.getName());
} else if (o1.getAge() == o2.getAge()) {
return o1.getAge() - o2.getAge();
} else {
return o1.getCountry().compareTo(o2.getCountry());
}
}
});

[解决办法]
需要实现Comparable接口

热点排行