代码如下:
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呢????? 谢谢大家了, 一鞠躬,二鞠躬!!!!!!!!!!!!!!!!!!!!!!
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呢????? 谢谢大家了, 一鞠躬,二鞠躬!!!!!!!!!!!!!!!!!!!!!!
解决方案 »
- this怎么用啊
- 新手问题一段代码
- 代码在内存中如何工作的,不知道我的理解对伐,请来看看。
- Float.floatToIntBits(float value) 是什么意思?
- 资源文件读取的问题(高手请帮忙)!
- 关于Java的生产者消费者问题
- 【请教】java中有没有将一个字符串重复N次功能类或方法
- 请问:this是什么?
- 谁有 http://tc2000.si.net.cn/wish.htm 这种源代码或知道怎么做的吗?
- 为什么用appletviewer正常, html里(已用HTMLconverter转换)却是nullpointerexception (jdk1.4B2)
- 关于java读文件的问题,请教
- 请教:急!!!!!服务器端连接客户端出现异常
然后实现compareTo方法.如果按名字比较就加上
public int compareTo(Object o) {
return name.compareTo(((Info2) o).getName());
}如果按年龄比较就加上
public int compareTo(Object o) {
return age - ((Info2)o).getAge();
}
或者什么其他的方法比较.
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;
} 光顾移动指针了
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;
}
});
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());
}
}
});