A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 银离子 中级黑马   /  2014-12-20 23:43  /  1385 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

把对象存到集合中,再存到文件里,for循环为什么不执行??
  1. import java.io.*;
  2. import java.util.*;
  3. //import java.lang.*;
  4. class person implements Comparable<person>
  5. {
  6.         private String name;
  7.         private int age;
  8.         private int text;
  9.         person(String name,int age,int text){
  10.                 this.name=name;
  11.                 this.age=age;
  12.                 this.text=text;
  13.         }
  14.         public int hashCode(){
  15.                 return name.hashCode()+age;
  16.         }
  17.         public boolean equals(Object obj){
  18.                 if(!(obj instanceof person))
  19.                 {
  20.                 }
  21.                 person p=(person)obj;
  22.                 return this.name.equals(p.name)&&this.age==p.age;
  23.         }
  24.         public int compareTo (person a){
  25.                 person s=a;
  26.                 if(this.age==s.age)
  27.                         return this.name.compareTo(s.name);
  28.                         return this.age-s.age;
  29.                
  30.                
  31.         }
  32.         public String tostring(){
  33.                 return this.name+"   "+(this.age+"")+"   "+(this.text+"");
  34.         }
  35.        
  36.        
  37. }
  38. public class zifudemo {

  39.         /**
  40.          * @param args
  41.          */
  42.        
  43.         public static void main(String[] args) throws IOException
  44.         {// TODO Auto-generated method stub
  45.                 //demo();
  46.                 TreeSet<person> sss=new TreeSet<person>();
  47.                 File file=new File("E:\\javatext\\javademo3.txt");
  48.                 rinfo(sss);
  49.                 w2file(sss,file);
  50.                        
  51.         }
  52.         public static void rinfo(TreeSet<person> set)throws IOException
  53.         {       
  54.                 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  55.                 String s=null;
  56.                  set=new TreeSet<person>();
  57.                 while(true){
  58.                         s=br.readLine();
  59.                         if(s.equals("over"))
  60.                         {
  61.                                 break;
  62.                         }
  63.                         String [] st=s.split(" ");
  64.                         System.out.println("执行到了while");
  65.                         set.add(new person(st[0],Integer.parseInt(st[1]),Integer.parseInt(st[2])));
  66.                        
  67.                 }
  68.                 System.out.println("执行到了rinfo");
  69.                 br.close();
  70.                
  71.                
  72.         }
  73.         public static void w2file(Set<person> s,File f)throws IOException
  74.         {
  75.                 BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f,true)));
  76.                
  77.                 for(person ss:s)
  78.                 {
  79.                 bw.write(ss.tostring());
  80.                 bw.newLine();
  81.                 bw.flush();
  82.                 System.out.println("执行到for");
  83.                 }
  84.                 bw.close();
  85.                
  86.                
  87.                 System.out.println("执行到w2file");
  88.                
  89.         }
  90.        
复制代码

评分

参与人数 1黑马币 +3 收起 理由
杨佳名 + 3

查看全部评分

2 个回复

倒序浏览
w2file方法中的for循环
回复 使用道具 举报
本帖最后由 奋斗的蜗牛ksd 于 2014-12-21 01:56 编辑

问题就出在 你的add方法理解错误!
add 方法里面参数列表不匹配
如果想添加对象的元素 这样添加 add(new Person(  参数列表 参数类型一定要匹配));

API:add
public boolean add(E e)将指定的元素添加到此 set(如果该元素尚未存在于 set 中)。更确切地讲,如果该 set 不包含满足 (e==null ? e2==null : e.equals(e2)) 的元素 e2,则将指定元素 e 添加到此 set 中。如果此 set 已经包含这样的元素,则该调用不改变此 set 并返回 false。

电脑没电了 2点啦。你的问题我只粗略看了,粗略分析,有疑问可以交流
白天有时间在认真看看。

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马