讨论Java的数组和集合操作

很简单的数组和集合操作. 在打印出结果之前, 请先预料一下, 注意一下注释和代码中红色的部分. 欢迎给出您的看法.  谢谢!   

java 代码
  1. public static void main(String[] args) {   
  2.         LabelValueBean[] lvBeans = new LabelValueBean[3];   
  3.         lvBeans[0] = new LabelValueBean("l0","v0");   
  4.         lvBeans[1] = new LabelValueBean("l1","v1");   
  5.         lvBeans[2] = new LabelValueBean("l2","v2");   
  6.            
  7.         Map<String, LabelValueBean> arrMap = new HashMap<String, LabelValueBean>();   
  8.            
  9.         arrMap.put("lv0", lvBeans[0]);   
  10.         arrMap.put("lv1", lvBeans[1]);   
  11.         arrMap.put("lv2", lvBeans[2]);   
  12.            
  13.         for(int i = 0;i < lvBeans.length;i ++) {   
  14.             logInfo("before: " + lvBeans[i].getLabel());   
  15.         }   
  16.   
  17.    /**  
  18.      *Collection 的 Object[] toArray() 方法有这样的注释:(注意加粗部分)  
  19.      *  
  20.      * Returns an array containing all of the elements in this collection.  If  
  21.      * the collection makes any guarantees as to what order its elements are  
  22.      * returned by its iterator, this method must return the elements in the  
  23.      * same order.<p>  
  24.      *  
  25.      * The returned array will be "safe" in that no references to it are  
  26.      * maintained by this collection.  (In other words, this method must  
  27.      * allocate a new array even if this collection is backed by an array).  
  28.      * The caller is thus free to modify the returned array.<p>  
  29.      *  
  30.      * This method acts as bridge between array-based and collection-based  
  31.      * APIs.  
  32.      *  
  33.      * @return an array containing all of the elements in this collection  
  34.      */  
  35.        
  36.   
  37.         Object[] lv1 = (Object[]) arrMap.values().toArray();   
  38.   
  39.         for(int i = 0;i < lv1.length;i ++) {   
  40.             LabelValueBean lv = (LabelValueBean)lv1[i];   
  41.             lv.setLabel("++");   
  42.             logInfo("Modifying: " + lv.getLabel());   
  43.         }   
  44.   
  45.         for(int i = 0;i < lvBeans.length;i ++) {   
  46.             logInfo("after: " + lvBeans[i].getLabel());   
  47.         }   
  48.     }   
  49.   
  50.     public static void logInfo(String str) {   
  51.         System.out.println("-----Info:\n " + str);   
  52.     }   

 

另: LabelValueBean 定义

java 代码
  1. public class LabelValueBean {   
  2.     private String label;   
  3.     private String value;   
  4.        
  5.     public LabelValueBean() {   
  6.            
  7.     }   
  8.        
  9.     public LabelValueBean(String label, String value) {   
  10.         this.label = label;   
  11.         this.value = value;   
  12.     }   
  13.   
  14.     public String getLabel() {   
  15.         return label;   
  16.     }   
  17.   
  18.     public void setLabel(String label) {   
  19.         this.label = label;   
  20.     }   
  21.   
  22.     public String getValue() {   
  23.         return value;   
  24.     }   
  25.   
  26.     public void setValue(String value) {   
  27.         this.value = value;   
  28.     }   
  29.   
  30. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值