Java使用list集合remove需要注意的事項(使用示例)
在實際開發(fā)中有時候會碰到這樣的場景,需要將一個list集合中的某些特定的元素給刪除掉,這個時候用可以用List提供的remove方法來實現(xiàn)需求。
List中的remove方法傳入的參數(shù)可以是集合的下標
,也可以是集合中一個元素
,也可以是一個集合
。
錯誤使用示例一:
這里我使用的是增強for循環(huán),會發(fā)現(xiàn)直接報錯。
List集合的一個特點是它其中的元素時有序的,也就是說元素的下標是根據插入的順序來的,在刪除頭部或者中間的一個元素后,后面的元素下標會往前移動。循環(huán)的時候就會出現(xiàn)問題。
@Data @AllArgsConstructor public class Person { private String id; private String name; public static void main(String[] args) { List<Person> lists = new ArrayList<>(); lists.add(new Person("1", "張三")); lists.add(new Person("2", "王五")); lists.add(new Person("3", "李六")); lists.add(new Person("4", "哈哈")); for (Person person4 : lists) { if (person4.getId().equals("2")) { lists.remove(person4); } } } }
解決方案一:
不要用for-each遍歷,換成迭代器遍歷,并且不要用list.remove()方法移除對象,用迭代器的方法iterator.remove()移除對象。
@Data @AllArgsConstructor public class Person { private String id; private String name; public static void main(String[] args) { List<Person> lists = new ArrayList<>(); lists.add(new Person("1", "張三")); lists.add(new Person("2", "王五")); lists.add(new Person("3", "李六")); lists.add(new Person("4", "哈哈")); Iterator<Person> iterator = lists.iterator(); while (iterator.hasNext()){ Person next = iterator.next(); if(next.getId().equals("2")){ iterator.remove(); } } lists.forEach(item-> System.out.println(item)); } }
解決方案二:
在循環(huán)中(比如for循環(huán))使用remove方法時,注意要從List集合的最后一個元素開始遍歷。
@Data @AllArgsConstructor public class Person { private String id; private String name; public static void main(String[] args) { List<Person> lists = new ArrayList<>(); lists.add(new Person("1", "張三")); lists.add(new Person("2", "王五")); lists.add(new Person("3", "李六")); lists.add(new Person("4", "哈哈")); for (int i = lists.size() - 1; i >= 0; i--) { if (lists.get(i).getId().equals("2")) { lists.remove(i); } } lists.forEach(item-> System.out.println(item)); } }
錯誤使用示例二:
這個示例當中沒有使用增強for,而是普通的循環(huán),沒有報錯,但是結果不是我們想要的結果。
public static void main(String[] args) { List<String> strList = new ArrayList<>(); strList.add("a"); strList.add("b"); strList.add("c"); strList.add("d"); strList.add("e"); strList.add("f"); for (int i = 0; i < strList.size(); i++) { if (i % 2 == 0) { strList.remove(i); } } for (String str : strList) { System.out.print(str + " "); } }
這個示例是我要把集合坐標為2的元素給刪除掉,按正常來說輸出b d f
才是我們想要的結果,顯然結果是不對的。
分析結果原因:
a b c d e f
0 1 2 3 4 5 刪除0坐標的a,然后后面元素向前移動
b c d e f
0 1 2 3 4 當i=1的時候不刪除,這時候i為2,刪除d
b c e f
0 1 2 3 刪除完d之后,后面向前移動,當i為3時候不用刪除,循環(huán)結束。
注意
:假如使用增強for就會報上面案例當中的錯誤。
for (String a : strList) { if (a.equals("a")) { strList.remove(a); } }
解決方案:
倒著循環(huán)list即可。
public static void main(String[] args) { List<String> strList = new ArrayList<>(); strList.add("a"); strList.add("b"); strList.add("c"); strList.add("d"); strList.add("e"); strList.add("f"); for (int i = strList.size() - 1; i >= 0; i--) { if (i % 2 == 0) { strList.remove(i); } } for (String str : strList) { System.out.print(str + " "); } }
到此這篇關于Java使用list集合remove需要注意的事項的文章就介紹到這了,更多相關java list集合remove內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java使用正則表達式實現(xiàn)找出數(shù)字功能示例
這篇文章主要介紹了Java使用正則表達式實現(xiàn)找出數(shù)字功能,結合實例形式分析了Java針對數(shù)字的匹配查找及非數(shù)字替換操作相關實現(xiàn)技巧,需要的朋友可以參考下2017-03-03Java使用Arrays.sort()方法實現(xiàn)給對象排序
這篇文章主要介紹了Java使用Arrays.sort()方法實現(xiàn)給對象排序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12maven中profile動態(tài)打包不同環(huán)境配置文件的實現(xiàn)
開發(fā)項目時會遇到這個問題:開發(fā)環(huán)境,測試環(huán)境,生產環(huán)境的配置文件不同, 打包時經常要手動更改配置文件,本文就來介紹一下maven中profile動態(tài)打包不同環(huán)境配置文件的實現(xiàn),感興趣的可以了解一下2023-10-10Mybatis或Mybatis-Plus框架的xml文件中特殊符號的使用詳解
這篇文章主要介紹了Mybatis或Mybatis-Plus框架的xml文件中特殊符號的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11springboot3.2整合mybatis-plus詳細代碼示例
這篇文章主要給大家介紹了關于springboot3.2整合mybatis-plus的相關資料,Spring Boot是一個非常流行的Java Web框架,可以快速地搭建Web應用程序,需要的朋友可以參考下2023-12-12