在for迴圈中remove list報錯越界的問題
阿新 • • 發佈:2019-02-01
最近在搞一個購物車的功能,裡面有一個批量刪除的操作,採用的是ExpandableListView以及BaseExpandableListAdapter。視乎跟本篇無關緊要,主要是為了記錄一個java基礎。迭代器iterator的使用
一、錯誤程式碼(主要就是購物車的批量刪除)
/**
* 刪除選中的
*/
public void delSelect() {
int groupSize;
if (mGropBeens != null) {
groupSize = mGropBeens.size();
} else {
return;
}
for (int i = 0; i < groupSize; i++) {
int childSize = mGropBeens.get(i).getChildBean().size();
for (int j = 0; j < childSize; j++) {
if (mGropBeens.get(i).getChildBean().get(j).isChecked()) {
DataSupport.deleteAll(ShopcartBean.class, "product_id=?" , mGropBeens.get(i).getChildBean().get(j).getProduct_id());
mGropBeens.get(i).getChildBean().remove(j);
}
}
}
notifyDataSetChanged();
}
分析一、其實就是一個迴圈遍歷list進行remove的操作,後來我分析了一下,錯誤的很明顯,就是你remove了這個list以後,list的長度就改變了,然後你繼續遍歷,就會報錯。感覺躲不了啊。錯誤有了,我覺得無法下手,後面既然remove不了,我就先刪除本地資料庫的方式,然後遍歷對data進行賦值。。。躲一下
/**
* 刪除選中的
*/
public void delSelect() {
int groupSize;
if (mGropBeens != null) {
groupSize = mGropBeens.size();
} else {
return;
}
for (int i = 0; i < groupSize; i++) {
int childSize = mGropBeens.get(i).getChildBean().size();
for (int j = 0; j < childSize; j++) {
if (mGropBeens.get(i).getChildBean().get(j).isChecked()) {
DataSupport.deleteAll(ShopcartBean.class, "product_id=?", mGropBeens.get(i).getChildBean().get(j).getProduct_id());
// mGropBeens.get(i).getChildBean().remove(j);
}
}
}
//重新整理資料來源
for (int i = 0; i < mGropBeens.size(); i++) {
mGropBeens.get(i).getChildBean().clear();
List<ShopcartBean> shopcartBeanlists = DataSupport.where("top_category_id=?", mGropBeens.get(i).getGroupId()).find(ShopcartBean.class);
mGropBeens.get(i).setChildBean(shopcartBeanlists);
}
notifyDataSetChanged();
}
分析二、寫了這樣以後還是感覺很不爽啊。明明我都迴圈了一遍知道要刪除這個物件了,還要等遍歷完,僅僅改變它的介面卡的data。感覺不爽,隨意的百度了下,發現有專門的解決方案,就是迭代器iterator
二、爭取的開啟方式
/**
* 刪除選中的
*/
public void delSelect() {
int groupSize;
if (mGropBeens != null) {
groupSize = mGropBeens.size();
} else {
return;
}
for (int i = 0; i < groupSize; i++) {
Iterator<ShopcartBean> iterator = mGropBeens.get(i).getChildBean().iterator();
while (iterator.hasNext()) {
ShopcartBean shopcartBean = iterator.next();
if (shopcartBean.isChecked()) {
DataSupport.deleteAll(ShopcartBean.class, "product_id=?", shopcartBean.getProduct_id());
iterator.remove();
}
}
}
notifyDataSetChanged();
}
分析三、發現這個玩意感覺還是很驚喜的,同時也感嘆自己基礎薄弱了。
一般迴圈for和foreach都需要先知道集合的型別,甚至是集合內元素的型別,即需要訪問內部的成員;iterator是一個介面型別,他不關心集合或者陣列的型別,而且他還能隨時修改和刪除集合的元素。他不包含任何有關他所遍歷的序列的型別資訊,能夠將遍歷序列的操作與序列底層的 結構分離。
基本使用模式就是:
List<object> arr=xxx;//把你的list賦值過來
Iterator it = arr.iterator();
while(it.hasNext()){
object o =it.next();//當前遍歷物件
iterator.remove();//刪除或修改等等
}
三、ok,先做記錄,後面繼續深入。have a nice day.這就是在一個list中刪除多個元素的正確解法。