java list<> get 出错_Java ArrayList 踩坑记录

做编程的一个常识:不要在循环过程中删除元素本身(至少是我个人的原则)。否则将发生不可预料的问题。

而最近,看到一个以前的同学写的一段代码就是在循环过程中删除元素,我很是纳闷啊。然后后来决定给他改掉。然后引发了另外的惨案。

原来的代码是这样的:

public ListgetUserDebitCard(A cond) {

List list = userService.getCard(cond);

List result = null;if(list! = null && list.size() > 0){

Collections.sort(list,new Comparator(){public intcompare(A b1, A b2) {//按时间排序

if(Integer.valueOf(b1.getAddTime()) >Integer.valueOf(b2.getAddTime())){return -1;

}return 1;

}

});

A bean=getA(cond);

result= new ArrayList();if(bean!=null){for (int i = 0; i < list.size(); i++) {if(list.get(i).getCardNum().equals(bean.getCardNum())){

list.get(i).setAs(1);

result.add(list.get(i));

list.remove(i);

}else{

list.get(i).setAs(0);

}

}

}

result.addAll(list);

}returnresult;

}

看了如上代码,我很是郁闷,然后给改成如下:

public ListgetUserDebitCard(A cond) {

List list=userService.getCard(cond);

List result=null;if(list!=null && list.size()>0){

Collections.sort(list,new Comparator(){public intcompare(A b1, A b2) {//按时间排序

if(Integer.valueOf(b1.getAddTime()) >Integer.valueOf(b2.getAddTime())){return -1;

}return 1;

}

});

A bean=getA(cond);

result=new ArrayList<>();if(bean != null){//将上次的卡放置在第一位

Integer lastAIndex = 0;

Integer listSize=list.size();if(listSize > 0) {for (int i = 0; i < listSize; i++) {if(list.get(i).getCardNum().equals(bean.getCardNum())) {

list.get(i).setAs(1);

result.add(list.get(i));//将排在首位的元素先添加好,并记录下index

lastAIndex =i;//list.remove(i);//循环过程中删除元素是危险的

} else{

list.get(i).setAs(0);

}

}

list.remove(lastAIndex);

}

}

result.addAll(list);//在循环外删除元素,以为万事大吉,结果悲剧了,这里居然添加了两个元素进来

}returnresult;

}

这下出事了,原本只有一个元素的result,现在变成了两个了,这是为什么呢?妈蛋,我明明已经remove掉了啊。

也想过百度一下,但是木有搞定啊。然后,拿出看家绝招,断点调试,进入list.remove(Integer) 方法。其源码如下:

/*** Removes the first occurrence of the specified element from this list,

* if it is present. If the list does not contain the element, it is

* unchanged. More formally, removes the element with the lowest index

* i such that

* (o==null ? get(i)==null : o.equals(get(i)))

* (if such an element exists). Returns true if this list

* contained the specified element (or equivalently, if this list

* changed as a result of the call).

*

*@paramo element to be removed from this list, if present

*@returntrue if this list contained the specified element*/

public booleanremove(Object o) {if (o == null) {for (int index = 0; index < size; index++)if (elementData[index] == null) {

fastRemove(index);return true;

}

}else{for (int index = 0; index < size; index++)if(o.equals(elementData[index])) {

fastRemove(index);return true;

}

}return false;

}

原来,由于我使用Integer作为删除元素的条件,这里把Integer当作一个元素去比较了,而并不是平时我们以为的自动拆装箱变为int了。 而进入这个方法的意思,是要找到和元素内容相等的元素,然后删除它。而我给的是一个索引,自然就不相等了,没有删除也是自然了,因为就会看到重复的元素出来了。

我们再来看一下根据索引删除元素的源码,对比一下:

/*** Removes the element at the specified position in this list.

* Shifts any subsequent elements to the left (subtracts one from their

* indices).

*

*@paramindex the index of the element to be removed

*@returnthe element that was removed from the list

*@throwsIndexOutOfBoundsException {@inheritDoc}*/

public E remove(intindex) {

rangeCheck(index);

modCount++;

E oldValue=elementData(index);int numMoved = size - index - 1;if (numMoved > 0)

System.arraycopy(elementData, index+1, elementData, index,

numMoved);

elementData[--size] = null; //clear to let GC do its work

returnoldValue;

}

从这里我们可以看到,根据索引删除ArrayList元素的原理是,将原来的数组排除要删除的索引,然后复制到新的数组去,然后将最后一个元素置为null,留给GC回收。并把删除的元素返回。

这样,一下就明白了删除ArrayList元素到底是咋么一回事了。修复方案就简单了,将Integer替换为int即可:

//Integer lastAIndex = 0;//替换成int

int lastAIndex = 0;

当然了,最后,我还是换成了另一个更稳妥的方案了,用另一个容器接收最终的结果

public ListgetUserDebitCard(A cond) {

List list = userService.getCard(cond);

List result = null;if(list! = null && list.size() > 0){

Collections.sort(list,new Comparator(){public intcompare(A b1, A b2) {//按时间排序

if(Integer.valueOf(b1.getAddTime()) >Integer.valueOf(b2.getAddTime())){return -1;

}return 1;

}

});

A bean=getA(cond);

result= new ArrayList<>();if(bean != null){//将上次的卡放置在第一位

for(A card1 : list) {if(card1.getCardNum().equals(bean.getCardNum())) {

card1.setAs(1);

result.add(0, card1); //直接插入第一位即可//list.remove(i);//循环过程中删除元素是危险的

} else{

card1.setAs(0);

result.add(card1);

}

}

}

}returnresult;

}

虽然自动拆装箱很方便,也很实用,但是有时一不小心就会把自己给埋坑里了,当心了。尤其是针对线上问题。

毕竟,代码中的一点点小问题,一到线上就会被无限放大,不可掉以轻心啊!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值