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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 294645832 中级黑马   /  2015-1-29 15:12  /  905 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package mapDemo;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Map.Entry;
  8. import java.util.Set;

  9. public class AccountDemo {
  10.         public static void main(String[] args) {
  11.                 List<Account> list = new ArrayList<Account>();
  12.                 list.add(new Account(10, "1234"));
  13.                 list.add(new Account(15, "5678"));
  14.                 list.add(new Account(0, "1010"));
  15.                
  16.                 Map<Long, Account> map = new HashMap<>();
  17.                 for(int i =0;i<list.size();i++){
  18.                         Account a = list.get(i);
  19.                         map.put(a.getId(), a);
  20.                 }
  21.                
  22.                 Set<Map.Entry<Long, Account>> entry = map.entrySet();
  23.                 Iterator<Entry<Long, Account>> iter = entry.iterator();
  24.                 while (iter.hasNext()) {
  25.                         Map.Entry<java.lang.Long,mapDemo.Account> entry2 = (Map.Entry<java.lang.Long,mapDemo.Account>) iter
  26.                                         .next();
  27.                         Long id = entry2.getKey();
  28.                         Account a = entry2.getValue();
  29.                         String password = a.getPassword();
  30.                         double balance = a.getBalance();
  31.                        
  32.                         System.out.println("id="+id+",password="+password+",balance="+balance);
  33.                 }
  34.                
  35.                
  36.         }
  37. }
  38. class Account{
  39.         private Long id =0l;
  40.         private double balance;
  41.         private String password;

  42.         public Account(double balance, String password) {
  43.                 super();
  44.                 ++id;
  45.                 this.balance = balance;
  46.                 this.password = password;
  47.         }
  48.         public Long getId() {
  49.                 return id;
  50.         }
  51.         public void setId(Long id) {
  52.                 this.id = id;
  53.         }
  54.         public double getBalance() {
  55.                 return balance;
  56.         }
  57.         public void setBalance(double balance) {
  58.                 this.balance = balance;
  59.         }
  60.         public String getPassword() {
  61.                 return password;
  62.         }
  63.         public void setPassword(String password) {
  64.                 this.password = password;
  65.         }
  66.        
  67. }
复制代码
为什么只有id=1,password=1010,balance=0.0
没有
                list.add(new Account(10, "1234"));
                list.add(new Account(15, "5678"));
                list.add(new Account(0, "1010"));

1 个回复

倒序浏览
默默顶一个。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马