qq_45735316
2020-10-28 20:39
采纳率: 85.7%
浏览 72
已采纳

为什么最后一个循环没有输出?是哪里错了?

User.java

package lesson06;

public class User implements Comparable<User>{
       private String id;
       private String gender;
       private String name;
       public User(){}
       public User(String id,String name,String gender)
       {
           this.id=id;
           this.name=name;
           this.gender=gender;
       }

       public void setid(String id)
       {
           this.id=id;
       }
       public String getid()
       {
           return this.id;
       }

       public void setname(String name)
       {
           this.name=name;
       }
       public String getname()
       {
           return this.name;
       }

       public void setgender(String name)
       {
           this.gender=gender;
       }
       public String getgender()
       {
           return this.gender;
       }

       public String toString()
       {
           return ("User:id="+this.getid()+" name="+this.getname()+" gender="+this.getgender());
       }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
    @Override
    public int compareTo(User o) {
        return this.compareTo(o);
    }

}

TestUser.java

package lesson06;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;


public class TestUser {
    public static String getsex(Random r)
    {
        int i=r.nextInt(2);//随机生成数的范围为[0,a)
        if(i==0)return "mam";
        else return "female";
    }
    public static void main(String[] args) {
        List<User> list=new ArrayList();
        Set<User> set=new HashSet();
        Map<String,User> map=new TreeMap();
        Random r=new Random();
        for(int i=0;i<50;i++)
        {
            String id="user"+r.nextInt(100);
            String name="zhang"+r.nextInt(100);
            String gender=getsex(r);
            User user=new User(id,name,gender);
            list.add(user);
            set.add(user);
            map.put(user.getid(), user);

        }

        Iterator<User> it=list.iterator();
        while(it.hasNext())
        {
            System.out.println(it.next().toString());
        }
        System.out.println("list.size="+list.size());
        System.out.println();

        it=set.iterator();
        while(it.hasNext())
        {
            System.out.println(it.next().toString());
        }
        System.out.println("set.size="+set.size());
        System.out.println();

        Set<String> keys=map.keySet();
        Iterator<String> it2=keys.iterator();
        while(it.hasNext())
        {
            System.out.println((map.get(it2.next())).toString());
        }
        System.out.println("map.size="+keys.size());
        System.out.println();
    }


}

```![图片说明](https://img-ask.csdn.net/upload/202010/28/1603888783_479836.jpg)
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题