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

aaronran 2007-09-15 06:02:19
代码如下:
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呢????? 谢谢大家了, 一鞠躬,二鞠躬!!!!!!!!!!!!!!!!!!!!!!
...全文
1645 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
exitzhang 2007-09-17
  • 打赏
  • 举报
回复
需要实现Comparable接口
aniude 2007-09-17
  • 打赏
  • 举报
回复
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());
}
}
});
malligator 2007-09-17
  • 打赏
  • 举报
回复
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;
}

});
ddystar6045 2007-09-17
  • 打赏
  • 举报
回复
对于40000长度的LinkedList,平均移动耗去10000 ,这在排序中是很耗时的
ddystar6045 2007-09-17
  • 打赏
  • 举报
回复
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;
}

光顾移动指针了
gcrjgh 2007-09-16
  • 打赏
  • 举报
回复
用Collections类的sort排序方法必须要实现Comparable接口才可以.
zephyr_cc 2007-09-15
  • 打赏
  • 举报
回复
Info2实现Comparable接口
然后实现compareTo方法.

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

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

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧