java關(guān)于list集合做刪除操作時的坑及解決
關(guān)于list集合做刪除操作時的坑
一個ArrayList在做刪除操作時,如果你使用如下方式,會有什么問題?
import java.util.ArrayList;
import java.util.List;
public class DeleteList {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(3);
list.add(3);
list.add(4);
for(int i=0;i<list.size();i++){
if (list.get(i)==3) {
list.remove(i);
}
}
System.out.println(list);
}
}
結(jié)果:[1, 2, 3, 4]。
分析問題:為什么會是有一個3遺留了?因為在訪問到list[2]時,滿足等于3,進行刪除。此時,3后面的元素會向左移動一位,也就是第二個3進入到i=2處,然后i=3,刪除第三個3,這樣就會漏掉第二個3.
解決辦法
1、訪問list從后往前,從i=size-1至i=0,可以避免上述問題;
2、使用iterator迭代器。
Iterator it = list.iterator();
while (it.hasNext()) {
Integer p = (Integer) it.next();
if(p==3){
it.remove();
}
}
System.out.println(list);
結(jié)果:[1, 2, 4]
下面是迭代器的部分源碼:當(dāng)執(zhí)行remove方法時,將當(dāng)前位置賦值給游標(biāo)cursor,再進行next時,還是獲取當(dāng)前位置的元素,也就是下一個位置移動過來的元素。這樣就避免的遺漏元素。關(guān)鍵點是刪除時,當(dāng)前位置是i,游標(biāo)的位置是i+1,修改游標(biāo)的位置為i。
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
對List集合的常用操作
備注:內(nèi)容中代碼具有關(guān)聯(lián)性。
1.list中添加,獲取,刪除元素
添加方法是:.add(e)
獲取方法是:.get(index)
刪除方法是:.remove(index)
按照索引刪除;.remove(Object o)
按照元素內(nèi)容刪除;
List<String> person=new ArrayList<>();
person.add("jackie"); //索引為0 //.add(e)
person.add("peter"); //索引為1
person.add("annie"); //索引為2
person.add("martin"); //索引為3
person.add("marry"); //索引為4
person.remove(3); //.remove(index)
person.remove("marry"); //.remove(Object o)
String per="";
per=person.get(1);
System.out.println(per); ////.get(index)
for (int i = 0; i < person.size(); i++) {
System.out.println(person.get(i)); //.get(index)
}
2.list中是否包含某個元素
方法:.contains(Object o); 返回true或者false
List<String> fruits=new ArrayList<>();
fruits.add("蘋果");
fruits.add("香蕉");
fruits.add("桃子");
//for循環(huán)遍歷list
for (int i = 0; i < fruits.size(); i++) {
System.out.println(fruits.get(i));
}
String appleString="蘋果";
//true or false
System.out.println("fruits中是否包含蘋果:"+fruits.contains(appleString));
if (fruits.contains(appleString)) {
System.out.println("我喜歡吃蘋果");
}else {
System.out.println("我不開心");
}
3.list中根據(jù)索引將元素數(shù)值改變(替換)
注意 .set(index, element); 和 .add(index, element); 的不同;
String a="白龍馬", b="沙和尚", c="八戒", d="唐僧", e="悟空";
List<String> people=new ArrayList<>();
people.add(a);
people.add(b);
people.add(c);
people.set(0, d); //.set(index, element); //將d唐僧放到list中索引為0的位置,替換a白龍馬
people.add(1, e); //.add(index, element); //將e悟空放到list中索引為1的位置,原來位置的b沙和尚后移一位
//增強for循環(huán)遍歷list
for(String str:people){
System.out.println(str);
}
4.list中查看(判斷)元素的索引
注意:.indexOf(); 和 lastIndexOf()的不同;
List<String> names=new ArrayList<>();
names.add("劉備"); //索引為0
names.add("關(guān)羽"); //索引為1
names.add("張飛"); //索引為2
names.add("劉備"); //索引為3
names.add("張飛"); //索引為4
System.out.println(names.indexOf("劉備"));
System.out.println(names.lastIndexOf("劉備"));
System.out.println(names.indexOf("張飛"));
System.out.println(names.lastIndexOf("張飛"));
5.根據(jù)元素索引位置進行的判斷
if (names.indexOf("劉備")==0) {
System.out.println("劉備在這里");
}else if (names.lastIndexOf("劉備")==3) {
System.out.println("劉備在那里");
}else {
System.out.println("劉備到底在哪里?");
}
6.利用list中索引位置重新生成一個新的list(截取集合)
方法:
.subList(fromIndex, toIndex)
.size()該方法得到list中的元素數(shù)的和
List<String> phone=new ArrayList<>();
phone.add("三星"); //索引為0
phone.add("蘋果"); //索引為1
phone.add("錘子"); //索引為2
phone.add("華為"); //索引為3
phone.add("小米"); //索引為4
//原list進行遍歷
for(String pho:phone){
System.out.println(pho);
}
//生成新list
phone=phone.subList(1, 4); //.subList(fromIndex, toIndex) //利用索引1-4的對象重新生成一個list,但是不包含索引為4的元素,4-1=3
for (int i = 0; i < phone.size(); i++) { // phone.size() 該方法得到list中的元素數(shù)的和
System.out.println("新的list包含的元素是"+phone.get(i));
}
7.對比兩個list中的所有元素
兩個相等對象的equals方法一定為true, 但兩個hashcode相等的對象不一定是相等的對象
//1.<br>if (person.equals(fruits)) {
System.out.println("兩個list中的所有元素相同");
}else {
System.out.println("兩個list中的所有元素不一樣");
}
//2.
if (person.hashCode()==fruits.hashCode()) {
System.out.println("我們相同");
}else {
System.out.println("我們不一樣");
}
8.判斷l(xiāng)ist是否為空
//空則返回true,非空則返回false
if (person.isEmpty()) {
System.out.println("空的");
}else {
System.out.println("不是空的");
}
9.返回Iterator集合對象
System.out.println("返回Iterator集合對象:"+person.iterator());
10.將集合轉(zhuǎn)換為字符串
String liString="";
liString=person.toString();
System.out.println("將集合轉(zhuǎn)換為字符串:"+liString);
11.將集合轉(zhuǎn)換為數(shù)組
System.out.println("將集合轉(zhuǎn)換為數(shù)組:"+person.toArray());
12.集合類型轉(zhuǎn)換
//1.默認類型
List<Object> listsStrings=new ArrayList<>();
for (int i = 0; i < person.size(); i++) {
listsStrings.add(person.get(i));
}
//2.指定類型
List<StringBuffer> lst=new ArrayList<>();
for(String string:person){
lst.add(StringBuffer(string));
}
13.去重復(fù)
List<String> lst1=new ArrayList<>();
lst1.add("aa");
lst1.add("dd");
lst1.add("ss");
lst1.add("aa");
lst1.add("ss");
//方法 1.
for (int i = 0; i <lst1.size()-1; i++) {
for (int j = lst1.size()-1; j >i; j--) {
if (lst1.get(j).equals(lst1.get(i))) {
lst1.remove(j);
}
}
}
System.out.println(lst1);
//方法 2.
List<String> lst2=new ArrayList<>();
for (String s:lst1) {
if (Collections.frequency(lst2, s)<1) {
lst2.add(s);
}
}
System.out.println(lst2);
附完整代碼:
package MyTest01;
import java.util.ArrayList;
import java.util.List;
public class ListTest01 {
public static void main(String[] args) {
//list中添加,獲取,刪除元素
List<String> person=new ArrayList<>();
person.add("jackie"); //索引為0 //.add(e)
person.add("peter"); //索引為1
person.add("annie"); //索引為2
person.add("martin"); //索引為3
person.add("marry"); //索引為4
person.remove(3); //.remove(index)
person.remove("marry"); //.remove(Object o)
String per="";
per=person.get(1);
System.out.println(per); ////.get(index)
for (int i = 0; i < person.size(); i++) {
System.out.println(person.get(i)); //.get(index)
}
//list總是否包含某個元素
List<String> fruits=new ArrayList<>();
fruits.add("蘋果");
fruits.add("香蕉");
fruits.add("桃子");
//for循環(huán)遍歷list
for (int i = 0; i < fruits.size(); i++) {
System.out.println(fruits.get(i));
}
String appleString="蘋果";
//true or false
System.out.println("fruits中是否包含蘋果:"+fruits.contains(appleString));
if (fruits.contains(appleString)) {
System.out.println("我喜歡吃蘋果");
}else {
System.out.println("我不開心");
}
//list中根據(jù)索引將元素數(shù)值改變(替換)
String a="白龍馬", b="沙和尚", c="八戒", d="唐僧", e="悟空";
List<String> people=new ArrayList<>();
people.add(a);
people.add(b);
people.add(c);
people.set(0, d); //.set(index, element) //將d唐僧放到list中索引為0的位置,替換a白龍馬
people.add(1, e); //.add(index, element); //將e悟空放到list中索引為1的位置,原來位置的b沙和尚后移一位
//增強for循環(huán)遍歷list
for(String str:people){
System.out.println(str);
}
//list中查看(判斷)元素的索引
List<String> names=new ArrayList<>();
names.add("劉備"); //索引為0
names.add("關(guān)羽"); //索引為1
names.add("張飛"); //索引為2
names.add("劉備"); //索引為3
names.add("張飛"); //索引為4
System.out.println(names.indexOf("劉備"));
System.out.println(names.lastIndexOf("劉備"));
System.out.println(names.indexOf("張飛"));
System.out.println(names.lastIndexOf("張飛"));
//根據(jù)元素索引位置進行的判斷
if (names.indexOf("劉備")==0) {
System.out.println("劉備在這里");
}else if (names.lastIndexOf("劉備")==3) {
System.out.println("劉備在那里");
}else {
System.out.println("劉備到底在哪里?");
}
//利用list中索引位置重新生成一個新的list(截取集合)
List<String> phone=new ArrayList<>();
phone.add("三星"); //索引為0
phone.add("蘋果"); //索引為1
phone.add("錘子"); //索引為2
phone.add("華為"); //索引為3
phone.add("小米"); //索引為4
//原list進行遍歷
for(String pho:phone){
System.out.println(pho);
}
//生成新list
phone=phone.subList(1, 4); //.subList(fromIndex, toIndex) //利用索引1-4的對象重新生成一個list,但是不包含索引為4的元素,4-1=3
for (int i = 0; i < phone.size(); i++) { // phone.size() 該方法得到list中的元素數(shù)的和
System.out.println("新的list包含的元素是"+phone.get(i));
}
//對比兩個list中的所有元素
//兩個相等對象的equals方法一定為true, 但兩個hashcode相等的對象不一定是相等的對象
if (person.equals(fruits)) {
System.out.println("兩個list中的所有元素相同");
}else {
System.out.println("兩個list中的所有元素不一樣");
}
if (person.hashCode()==fruits.hashCode()) {
System.out.println("我們相同");
}else {
System.out.println("我們不一樣");
}
//判斷l(xiāng)ist是否為空
//空則返回true,非空則返回false
if (person.isEmpty()) {
System.out.println("空的");
}else {
System.out.println("不是空的");
}
//返回Iterator集合對象
System.out.println("返回Iterator集合對象:"+person.iterator());
//將集合轉(zhuǎn)換為字符串
String liString="";
liString=person.toString();
System.out.println("將集合轉(zhuǎn)換為字符串:"+liString);
//將集合轉(zhuǎn)換為數(shù)組,默認類型
System.out.println("將集合轉(zhuǎn)換為數(shù)組:"+person.toArray());
////將集合轉(zhuǎn)換為指定類型(友好的處理)
//1.默認類型
List<Object> listsStrings=new ArrayList<>();
for (int i = 0; i < person.size(); i++) {
listsStrings.add(person.get(i));
}
//2.指定類型
List<StringBuffer> lst=new ArrayList<>();
for(String string:person){
lst.add(StringBuffer(string));
}
}
private static StringBuffer StringBuffer(String string) {
return null;
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring的refresh()方法相關(guān)異常解析
這篇文章主要介紹了Spring的refresh()方法相關(guān)異常解析,具有一定參考價值,需要的朋友可以了解下。2017-11-11
SpringBoot整合Swagger接口文檔工具的流程步驟
我們在開發(fā)接口的時候,會將接口文檔給前端的開發(fā)者進行對接,我們可以通過Postman或者Yapi等接口管理工具進行編寫管理,實際開發(fā)中,接口的管理確實也應(yīng)該通過專業(yè)的工具管理,本文,我們就來談?wù)勗趺丛赟pringBoot整合Swagger接口文檔工具2023-08-08
使用jenkins+maven+git發(fā)布jar包過程詳解
這篇文章主要介紹了使用jenkins+maven+git發(fā)布jar包過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
詳解用Spring Boot Admin來監(jiān)控我們的微服務(wù)
這篇文章主要介紹了用Spring Boot Admin來監(jiān)控我們的微服務(wù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
解決feign接口返回泛型設(shè)置屬性為null的問題
這篇文章主要介紹了解決feign接口返回泛型設(shè)置屬性為null的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

