标题:打出来不对啊!
只看楼主
嶄ぎ儱
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2007-4-21
 问题点数:0 回复次数:4 
打出来不对啊!

为什么打出来 是这样的
student@17590db
student@17943a4
student@480457
import java.util.*;
class test2
{
public static void pp(Collection c)
{
Iterator i=c.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
public static void main(String[] args)
{
student s1=new student(1,"wanghai");
student s2=new student(3,"lihai");
student s3=new student(2,"jianghai");
ArrayList a1=new ArrayList();
a1.add(s1);
a1.add(s2);
a1.add(s3);
Collections.sort(a1);
pp(a1);
}
}
class student implements Comparable
{
int num;
String name;
student(int num,String name)
{
this.num=num;
this.name=name;
}
public int compareTo(Object o)
{
student s=(student)o;
return num>s.num ? 1 : (num==s.num ? 0 : -1);
}
public String ToString()
{
return num+":"+name;
}
}

搜索更多相关主题的帖子: blank public import target 
2007-04-22 11:09
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
得分:0 

自己重定义一个toString方法就OK了


可惜不是你,陪我到最后
2007-04-22 11:53
beyond_p
Rank: 1
等 级:新手上路
威 望:1
帖 子:114
专家分:0
注 册:2007-1-17
得分:0 
public String ToString()
{
return num+":"+name;
}

你定义的方法名写错了 没有重写toString方法
应该为toString

越是深入的学习,就越来越感觉到自己知道的太少了...
2007-04-22 12:05
changyawei
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2005-11-3
得分:0 

public class TextTest
{
int len=10000;// 求值范围
int l,m,n;
public TextTest()
{

}

public static void main(String args[])throws Exception
{
//TextTest tt = new TextTest();
Employee e1=new Employee("zhaobo",21);
Employee e2=new Employee("zhaoyunxia",20);
Employee e3=new Employee("baby",2);

FileOutputStream raf =new FileOutputStream("employee.txt");
ObjectOutputStream oos = new ObjectOutputStream(raf);
oos.writeObject(e1);
oos.writeObject(e2);
oos.writeObject(e3);
oos.flush();
raf.close();
oos.close();

FileInputStream raf2 =new FileInputStream("employee.txt");
ObjectInputStream ois = new ObjectInputStream(raf2);
Employee temp_oob = (Employee)ois.readObject();;
System.out.println(" "+temp_oob.getName()+" "+temp_oob.getAge());


temp_oob = (Employee)ois.readObject();;
System.out.println(" "+temp_oob.getName()+" "+temp_oob.getAge());

temp_oob = (Employee)ois.readObject();;
System.out.println(" "+temp_oob.getName()+" "+temp_oob.getAge());
raf2.close();
ois.close();
}



}


class Employee implements Serializable
{
private String name;
private int age;
public static final int LEN=14;

public Employee()
{

}
public Employee(String name,int age)
{
if(name.length()>LEN)
{
name=name.substring(0,LEN);
}
else
while(name.length()<LEN)
{
name+="\u0000";
}
this.name=name;
this.age=age;
}

//method: getName(),getAge()
public String getName()
{
return name;
}

public int getAge()
{
return age;
}

}

//你把Employee类序列化后,直接存取对象不是很好吗?

2007-04-22 12:13
嶄ぎ儱
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2007-4-21
得分:0 
谢谢各位了,能指出我错.!!

别人不给你机会,自己去创造机会.
2007-04-22 12:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-133959-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.672301 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved