代码如下:
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呢?????   谢谢大家了, 一鞠躬,二鞠躬!!!!!!!!!!!!!!!!!!!!!!

解决方案 »

  1.   

    Info2实现Comparable接口
    然后实现compareTo方法.如果按名字比较就加上
    public int compareTo(Object o) {
    return name.compareTo(((Info2) o).getName());
    }如果按年龄比较就加上
    public int compareTo(Object o) {
    return age - ((Info2)o).getAge();
    }
    或者什么其他的方法比较.
      

  2.   

    用Collections类的sort排序方法必须要实现Comparable接口才可以.
      

  3.   

    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;
        }    光顾移动指针了
      

  4.   

    对于40000长度的LinkedList,平均移动耗去10000 ,这在排序中是很耗时的
      

  5.   

    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;
    }

    });
      

  6.   

    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());
                    }
                }
            });