java ArrayList.remove()的三种错误用法以及六种正确用法详解
java集合中,list列表应该是我们最常使用的,它有两种常见的实现类:ArrayList和LinkedList。ArrayList底层是数组,查找比较方便;LinkedList底层是链表,更适合做新增和删除。但实际开发中,我们也会遇到使用ArrayList需要删除列表元素的时候。虽然ArrayList类已经提供了remove方法,不过其中有潜在的坑,下面将介绍remove方法的三种错误用法以及六种正确用法。
1、错误用法
1.1、for循环中使用remove(intindex),列表从前往后遍历
首先看一下ArrayList.remove(intindex)的源码,读代码前先看方法注释:移除列表指定位置的一个元素,将该元素后面的元素们往左移动一位。返回被移除的元素。
源代码也比较好理解,ArrayList底层是数组,size是数组长度大小,index是数组索引坐标,modCount是被修改次数的计数器,oldValue就是被移除索引的元素对象,numMoved是需要移动的元素数量,如果numMoved大于0,则执行一个数组拷贝(实质是被移除元素后面的元素都向前移动一位)。然后数组长度size减少1,列表最后一位元素置为空。最后将被移除的元素对象返回。
/** *Removestheelementatthespecifiedpositioninthislist. *Shiftsanysubsequentelementstotheleft(subtractsonefromtheir *indices). * *@paramindextheindexoftheelementtoberemoved *@returntheelementthatwasremovedfromthelist *@throwsIndexOutOfBoundsException{@inheritDoc} */ publicEremove(intindex){ rangeCheck(index); modCount++; EoldValue=elementData(index); intnumMoved=size-index-1; if(numMoved>0) System.arraycopy(elementData,index+1,elementData,index, numMoved); elementData[--size]=null;//cleartoletGCdoitswork returnoldValue; }
如果在for循环中调用了多次ArrayList.remove(),那代码执行结果是不准确的,因为每次每次调用remove函数,ArrayList列表都会改变数组长度,被移除元素后面的元素位置都会发生变化。比如下面这个例子,本来是想把列表中奇数位置的元素都移除,但最终得到的结果是[2,3,5]。
Listlist=newArrayList<>(Arrays.asList(1L,2L,3L,4L,5L)); for(inti=0;i 1.2、直接使用list.remove(Objecto)
ArrayList.remove(Objecto)源码的逻辑和ArrayList.remove(intindex)大致相同:列表索引坐标从小到大循环遍历,若列表中存在与入参对象相等的元素,则把该元素移除,后面的元素都往左移动一位,返回true,若不存在与入参相等的元素,返回false。
publicbooleanremove(Objecto){ if(o==null){ for(intindex=0;index0) System.arraycopy(elementData,index+1,elementData,index, numMoved); elementData[--size]=null;//cleartoletGCdoitswork } 如果直接对list调用了该方法,代码结果可能会不准确。例子如下:这段代码本想移除列表中全部值为2的元素,结果并没有成功。
Listlist=newArrayList<>(Arrays.asList(1L,2L,2L,4L,5L)); list.remove(2L); //最终得到[1,2,4,5] 1.3、Arrays.asList()之后使用remove()
为啥使用了Arrays.asList()之后使用remove是错误用法,我们看一下asList()的源码就能知道了。Arrays.asList()返回的是一个指定数组长度的列表,所以不能做Add、Remove等操作。至于为啥是返回的是固定长度的,看下面源码,asList()函数中调用的newArrayList<>()并不是我们常用的ArrayList类,而是一个Arrays的内部类,也叫ArrayList,而且这个内部类也是基于数组实现的,但它有一个明显的关键字修饰,那就是final。都用final修饰了,那是肯定不能再对它进行add/remove操作的。如果非要在Arrays.asList之后使用remove,正确用法参见2.5。
publicstaticList asList(T...a){ returnnewArrayList<>(a); } privatestaticclassArrayList extendsAbstractList implementsRandomAccess,java.io.Serializable { privatestaticfinallongserialVersionUID=-2764017481108945198L; privatefinalE[]a; ArrayList(E[]array){ a=Objects.requireNonNull(array); } } 2、正确用法
2.1、直接使用removeIf()
使用removeIf()这个方法前,我是有点害怕的,毕竟前面两个remove方法都不能直接使用。于是小心翼翼的看了removeIf函数的方法。确认过源码,是我想要的方法!
源码如下:removeIf()的入参是一个过滤条件,用来判断需要移除的元素是否满足条件。方法中设置了一个removeSet,把满足条件的元素索引坐标都放入removeSet,然后统一对removeSet中的索引进行移除。源码相对复杂的是BitSet模型,源码这里不再贴了。
publicbooleanremoveIf(Predicatefilter){ Objects.requireNonNull(filter); //figureoutwhichelementsaretoberemoved //anyexceptionthrownfromthefilterpredicateatthisstage //willleavethecollectionunmodified intremoveCount=0; finalBitSetremoveSet=newBitSet(size); finalintexpectedModCount=modCount; finalintsize=this.size; for(inti=0;modCount==expectedModCount&&i0; if(anyToRemove){ finalintnewSize=size-removeCount; for(inti=0,j=0;(i removeIf()的使用方法如下所示(jdk8),结果满足预期。
Listlist=newArrayList<>(Arrays.asList(1L,2L,2L,4L,5L)); list.removeIf(val->val==2L); //结果得到[1L,4L,5L] 2.2、在for循环之后使用removeAll(Collection>c)
这种方法思路是for循环内使用一个集合存放所有满足移除条件的元素,for循环结束后直接使用removeAll方法进行移除。removeAll源码如下,还是比较好理解的:定义了两个数组指针r和w,初始都指向列表第一个元素。循环遍历列表,r指向当前元素,若当前元素没有满足移除条件,将数组[r]元素赋值给数组[w],w指针向后移动一位。这样就完成了整个数组中,没有被移除的元素向前移动。遍历完列表后,将w后面的元素都置空,并减少数组长度。至此完成removeAll移除操作。
publicbooleanremoveAll(Collection>c){ Objects.requireNonNull(c); returnbatchRemove(c,false); } privatebooleanbatchRemove(Collection>c,booleancomplement){ finalObject[]elementData=this.elementData; intr=0,w=0; booleanmodified=false; try{ for(;r正确使用方式如下:
ListremoveList=newArrayList<>(); for(inti=0;i 2.3、list转为迭代器Iterator的方式
迭代器就是一个链表,直接使用remove操作不会出现问题。
Iteratorit=list.iterator(); while(it.hasNext()){ if(it.next()%2==0) it.remove(); } 2.4、for循环中使用remove(intindex),列表从后往前遍历
前面1.1也是for循环,为啥从后往前遍历就是正确的呢。因为每次调用remove(intindex),index后面的元素会往前移动,如果是从后往前遍历,index后面的元素发生移动,跟index前面的元素无关,我们循环只去和前面的元素做判断,因此就没有影响。
for(inti=list.size()-1;i>=0;i--){ if(list.get(i).longValue()==2){ list.remove(i); } }2.5、Arrays.asList()之后使用remove()
Arrays.asList()之后需要进行add/remove操作,可以使用下面这种方式:
String[]arr=newString[3]; Listlist=newArrayList(Arrays.asList(arr));2.6、使用while循环
使用while循环,删除了元素,索引便不+1,在没删除元素时索引+1
inti=0; while(i以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。