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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值