List差集

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * List工具类
 * <p>
 *   提供处理List对象的各种方法实现。
 * </p>
 * 
 * @author harbey
 * @version 1.0
 * @since 2009-07-30
 */
public class ListUtil {

	/**
	 * List对象差集
	 * <p>
	 *   使用深复制,不会影响List参数对象。
	 * </p>
	 * @param decList 被减数List对象
	 * @param subList 减数List对象
	 * @return 结果List
	 * @throws IllegalArgumentException
	 */
	public static List<String> plusArrayList(List<String> decList,List<String> subList) throws IllegalArgumentException{
		   
		   if(decList==null || subList==null){
			   throw new IllegalArgumentException();
		   }
	 
			List<String> tempDecList = new ArrayList<String>(new ArrayList(Arrays.asList(new Object[decList.size()])));
			List<String> tempSubList = new ArrayList<String>(new ArrayList(Arrays.asList(new Object[subList.size()])));
		   
			Collections.copy(tempDecList, decList);
			Collections.copy(tempSubList, decList);
			
			tempDecList.removeAll(tempSubList);
			
		    return tempDecList;
	   }
	   
}



如果
List<String> tempDecList = new ArrayList<String>(decList.size());
则会报java.lang.IndexOutOfBoundsException错误!
原因:
ArrayList   iss=new   ArrayList(ist.size());does   not   ensure   size,   but   set   the   initialize   of   the   internal   array.  


你可能感兴趣的:(java)