首先说明,使用如下方法是不可以的:
List list=new ArrayList<>();
for(int i = 0; i < ll.size(); i++){
list.add(ll.get(i));
ll.remove(i);
}
说明使用remove是不可行的,因为remove会删除i位置的元素,然后后面的元素统一向前移一位,这样你会发现,得到元素莫名的少了
废话不多说,首先看我的需求,
我有一个实体类,结构如下:
{
"start_date": 691200,
"start_time": "00:00",
"content": "哈哈"
},
完整的json太多,列出一部分:
"routes": [
{
"start_date": 0,
"start_time": "00:00",
"content": "5oiQ6YO96ZuG5ZCI"
},
{
"start_date": 0,
"start_time": "00:00",
"content": "6aSQ6aOfOuWFqOWkqemkkOi0ueiHqueQhg=="
},
{
"start_date": 0,
"start_time": "00:00",
"content": "5L2P5a6/OuaIkOmDveW4guWMuu+8jOmFkuW6lzItM+S6uuagh+WHhumXtA=="
},
{
"start_date": 86400,
"start_time": "00:00",
"content": "5oiQ6YO9LembheWuiS3kuozpg47lsbEt5rO45a6aLeW6t+Wumi3mlrDpg73moaUgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIOOAgg=="
},
{
"start_date": 86400,
"start_time": "00:00",
"content": "5L2P5a6/OuaWsOmDveahpeW9k+WcsOWuvummhu+8iOagh+mXtO+8iQ=="
},
{
"start_date": 86400,
"start_time": "00:00",
"content": "6aSQ6aWuOuazuOWumuS4remkkCzmlrDpg73moaXmmZrppJA="
},
{
"start_date": 86400,
"start_time": "00:00",
"content": "6ZyA5pe2OuaIkOmDvS3lurflrprnuqY2LjXlsI/ml7bvvIzlurflrpot5paw6YO95qGl57qmMy415bCP5pe2"
},
{
"start_date": 86400,
"start_time": "00:00",
"content": "5rW35ouUOuaIkOmDvTU0MOexs++8jOW6t+WumjI5OTDnsbPvvIzmipjlpJrlsbE0Mjk457Gz77yM5paw6YO95qGlMzQwMOexsw=="
},
{
"start_date": 259200,
"start_time": "00:00",
"content": "5a2Q5qKF5p2RLei0oeWYjuWvui3otKHlmI7lhrDlt50t5aSn5pys6JCl"
},
{
"start_date": 259200,
"start_time": "00:00",
"content": "6aSQ6aWuOuWtkOaiheadkeaXqemkkCDoh6rlpIfot6/ppJAg6JCl5Zyw5pma6aSQ"
},
{
"start_date": 259200,
"start_time": "00:00",
"content": "5L2P5a6/OuWkp+acrOiQpQ=="
},
{
"start_date": 259200,
"start_time": "00:00",
"content": "5rW35ouUOui0oeWYjuWvujM4NTBt44CB6JCl5ZywNDIwMG0="
},
{
"start_date": 345600,
"start_time": "00:00",
"content": "5aSn5pys6JClLemAguW6lOaAp+iuree7gy1DMe+8iDQ5MDDlsbHlnbPokKXlnLDvvIk="
},
{
"start_date": 345600,
"start_time": "00:00",
"content": "6aSQ6aWuOuiQpeWcsOaXqemkkOOAgei3r+mkkOOAgeiQpeWcsOaZmumkkA=="
},
{
"start_date": 345600,
"start_time": "00:00",
"content": "5L2P5a6/OumcsuiQpQ=="
},
{
"start_date": 345600,
"start_time": "00:00",
"content": "6ZyA5pe2OkJDLUMx57qm6ZyANOWwj+aXtg=="
},
{
"start_date": 345600,
"start_time": "00:00",
"content": "5rW35ouUOkMx5rW35ouU57qmNDkwMOexsw=="
}
]
就像这样的,我需要把start_date相同的组成一个新的list,但是有多少个list,每个list的长度是多少,都不确定,这样我们的思路还是取出一个删除一个,但是不能使用remove,这样会有问题,那么怎么删除呢?这就要用到Iterator(迭代),官方给的迭代器就可以完成这种操作,它的原理大致是:讲list拷贝一份给Iterator迭代器,然会便利迭代器,便利的时候边取值,边删除,删除完成后在更新list,这样list就不会因为同时取值和删除二有问题啦,好啦,看代码:
private void setDataToLv(List<RoutesBean> list){
List<List<RoutesBean>> ll=new ArrayList<>();
while (list.size()>0){
RoutesBean b=list.get(0);
List<RoutesBean> l=new ArrayList<>();
Iterator iterator=list.iterator();
while (iterator.hasNext()){
RoutesBean bean= (RoutesBean) iterator.next();
if (bean.getStartDate()==b.getStartDate()){
l.add(bean);
iterator.remove();
}
}
if (l.size()>0)
ll.add(l);
}
//打印测试
for (int i = 0; i < ll.size(); i++) {
Log.e("qqqqq",ll.get(i).get(0).getStartDate()+"=="+ll.get(i).size());
}
}