1. /**  
  2.  * 版权所有:华信设计  
  3.  * 项目名称:上海移动项目第三期  
  4.  * 创建者: Zhou Danyong  
  5.  * 创建日期: 2010-09-30  
  6.  * 文件说明: ArrayList 功能类  
  7.  * 最近修改者:Zhou Danyong  
  8.  * 最近修改日期:2010-12-23  
  9.  */ 
  10. package com.huaxin.shpm3.common.util;  
  11.  
  12. import java.util.ArrayList;  
  13. import java.util.HashMap;  
  14. import java.util.Iterator;  
  15. import com.huaxin.util.StringTool;  
  16.  
  17. /**  
  18.  * Tool for ArrayList.  
  19.  * Which inlcuded some functions like converts an Array to a comma-delimited String, as so on, you can append you  
  20.  * function in you your, make a stamp meanwhile.  
  21.  *   
  22.  * @author Zhou Danyong 2010-11-01  
  23.  * @since 1.1  
  24.  */ 
  25. @SuppressWarnings("unchecked")  
  26. public class ArrayListUtil  extends StringTool {  
  27.  
  28.       
  29.     /**  
  30.      * Delete the truble item from the list, and convert the reslut as an SQL String. Which can  
  31.      * used in SQL directly.  
  32.      *   
  33.      * @author Zhou Danyong 2010-11-01  
  34.      * @param list  
  35.      * @param trouble  
  36.      * @return safeString  
  37.      * @since 1.0  
  38.      */ 
  39.     public static String doKickOutStringThenConvertListToAnSQLString(ArrayList list, String trouble) {  
  40.         String wholeString = "",safeString = "";  
  41.         //1.Change List to SQL String  
  42.         for (Iterator iterator = list.iterator(); iterator.hasNext();) {  
  43.             HashMap map = (HashMap) iterator.next();  
  44.             wholeString +="'"+map.get("AAA")+"'";  
  45.             if (iterator.hasNext()) {  
  46.                 wholeString += ",";  
  47.             }  
  48.         }  
  49.         list = null;  
  50.           
  51.         //2.Kick out self  
  52.         //2.1.middle position  
  53.         if(StringUtil.countOccurrencesOf(wholeString,"'"+trouble+"'"+",")>0){  
  54.             safeString = StringUtil.delete(wholeString, "'"+trouble+"'"+",");  
  55.         //2.2.end position  
  56.         }else if(StringUtil.countOccurrencesOf(wholeString,trouble)>0){  
  57.             safeString = StringUtil.delete(wholeString, ","+"'"+trouble+"'");  
  58.         }  
  59.           
  60.         //3.Return   
  61.         return  safeString;  
  62.     }  
  63.       
  64.      /**  
  65.      * Imitate StringBuffer's append operation.  
  66.      * <p>  
  67.      * Appends the specified <tt>ArrayList</tt> to the target one.  
  68.      * </p>  
  69.      *  
  70.      * @author Zhou Danyong 2010-12-23  
  71.      * @param   targetList, appendList.  
  72.      * @return  a expend ArrayList.  
  73.      * @since 1.0  
  74.      */ 
  75.     public static ArrayList appendToArrayList(ArrayList targetList,ArrayList appendList){  
  76.         //Type check  
  77.         if (!ArrayList.class.isInstance(targetList)) {  
  78.             throw new RuntimeException("targetList must be ArrayList.");  
  79.         }  
  80.         if (!ArrayList.class.isInstance(appendList)) {  
  81.             throw new RuntimeException("appendList must be ArrayList.");  
  82.         }  
  83.           
  84.         //Append target  
  85.         ArrayList list = new ArrayList();  
  86.         list.addAll(targetList);  
  87.         int oldCapacity = appendList.size();  
  88.         if (oldCapacity>10) {   
  89.             int newCapacity = (oldCapacity * 3)/2 + 1;  
  90.             list.ensureCapacity(newCapacity);  
  91.         }  
  92.           
  93.         //Append  
  94.         int index = targetList.size();  
  95.         HashMap map = new HashMap();  
  96.         for (int i = 0; i < appendList.size(); i++) {  
  97.             map = (HashMap) appendList.get(i);  
  98.             list.add(index++, map);  
  99.         }  
  100.           
  101.         return list;  
  102.     }