淺談Java list.remove( )方法需要注意的兩個坑
list.remove
最近做項(xiàng)目的過程中,需要用到list.remove()方法,結(jié)果發(fā)現(xiàn)兩個有趣的坑,經(jīng)過分析后找到原因,記錄一下跟大家分享一下。
代碼
直接上一段代碼,進(jìn)行分析。
public class Main { public static void main(String[] args) { List<String> stringList = new ArrayList<>();//數(shù)據(jù)集合 List<Integer> integerList = new ArrayList<>();//存儲remove的位置 stringList.add('a'); stringList.add('b'); stringList.add('c'); stringList.add('d'); stringList.add('e'); integerList.add(2); integerList.add(4);//此處相當(dāng)于要移除最后一個數(shù)據(jù) for (Integer i :integerList){ stringList.remove(i); } for (String s :stringList){ System.out.println(s); } }}
如上代碼我們有一個5個元素的list數(shù)據(jù)集合,我們要刪除第2個和第4個位置的數(shù)據(jù)。
第一次運(yùn)行
咦,為什么執(zhí)行兩次remove(),stringList的數(shù)據(jù)沒有變化呢?
沒有報(bào)錯,說明代碼沒有問題,那問題出在哪呢?
仔細(xì)分析我們發(fā)現(xiàn),remove()這個方法是一個重載方法,即remove(int position)和remove(object object),唯一的區(qū)別是參數(shù)類型。
for (Integer i :integerList){ stringList.remove(i); }
仔細(xì)觀察上面代碼你會發(fā)現(xiàn),其實(shí)i是Integer對象,而由于Java系統(tǒng)中如果找不到準(zhǔn)確的對象,會自動向上升級,而(int < Integer < Object),所以在調(diào)用stringList.remove(i)時,其實(shí)使用的remove(object object),而很明顯stringList不存在Integer對象,自然會移除失敗(0.0),Java也不會因此報(bào)錯。
如果我們想使用remove(int position)方法,只能降低對象等級,即修改代碼;
for (Integer i :integerList){ int a =i; stringList.remove(a); }
第二次運(yùn)行
我們發(fā)現(xiàn)提示在坐標(biāo)為4的地方越界了,這是為什么呢?
其實(shí)很簡單,因?yàn)閳?zhí)行stringList.remove(2)后,list.size()就-1為4了,我們原來要移除的最后一個位置的數(shù)據(jù)移動到了第3個位置上,自然就造成了越界。
我們修改代碼先執(zhí)行stringList.remove(4),再執(zhí)行執(zhí)行stringList.remove(2)。
integerList.add(4);
integerList.add(2);
這個錯誤提醒我們:使用remove()的方法時,要先從大到小的位置移除。當(dāng)然如果你知道具體的對象,直接移除remove(對象)更穩(wěn)妥。
第三次執(zhí)行
嗯,這次沒問題了。
總結(jié)
1、使用remove()的方法時,要先從大到小的位置移除。當(dāng)然如果你知道具體的對象,直接移除remove(對象)更穩(wěn)妥。
2、要密切注意自己調(diào)用的remove()方法中的,傳入的是int類型還是一個對象。
補(bǔ)充知識: 關(guān)于List.remove()報(bào)錯的問題
我們?nèi)绻雱h掉List中某一個對象,我們可能就會想到會用List.remove()方法。但是這樣如果后續(xù)操作這個list的時候就會報(bào)錯。
具體的原因是當(dāng)你操作了List的remove方法的時候,他回去修改List的modCount屬性。
導(dǎo)致拋出異常java.util.ConcurrentModificationException。
最好的想要修改List對象,我們可以用ListIterator。
就像這樣:
ArrayList<Integer> arrayList = new ArrayList<>(); for (int i = 0; i < 20; i++) { arrayList.add(Integer.valueOf(i)); }ListIterator<Integer> iterator = arrayList.listIterator();while (iterator.hasNext()) {if(需要滿足的條件){iterator.remove();//刪除操作iterator.add(integer);//新增操作}}
這樣就不會去修改List的modCount屬性。
以上這篇淺談Java list.remove( )方法需要注意的兩個坑就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 存儲于xml中需要的HTML轉(zhuǎn)義代碼2. python 浮點(diǎn)數(shù)四舍五入需要注意的地方3. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法4. python開發(fā)一款翻譯工具5. 利用CSS制作3D動畫6. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))7. Springboot 全局日期格式化處理的實(shí)現(xiàn)8. 完美解決vue 中多個echarts圖表自適應(yīng)的問題9. SpringBoot+TestNG單元測試的實(shí)現(xiàn)10. JAMon(Java Application Monitor)備忘記
