天天看點

【每日知識點】20200811 interator hashmap(未填)

1.疊代器 interator

【基礎】

功能比較簡單,并且隻能單向移動

包含hasnext、next、remove 三個方法

  • hasnext:檢查序列中是否還有元素
  • next:取得目前值,并指向下一個值
  • remove:将新傳回元素删除,請注意:由于是删除新傳回元素,是以要删除第一個元素時,必須先next,否則會報錯

在要删除元素時,不要使用原集合進行删除,而是使用疊代器删除!

個人了解,疊代器像指針這個指針已經連接配接在這個集合上了,再更改就打亂排序了,是以不可以!

【題】

1.list 是一個 ArrayList 的對象,哪個選項的代碼填到 //todo delete 處,可以在 Iterator 周遊的過程中正确并安全的删除一個 list 中儲存的對象

Iterator it = list.iterator();
int index = 0;
while (it.hasNext())
{
    Object obj = it.next();
    if (needDelete(obj))  //needDelete傳回boolean,決定是否要删除
    {
        //todo delete
    }
    index ++;
}
           

答案:it.remove();

注意不能選list.remove();

原因你猜:-)

【參考連接配接】

https://blog.csdn.net/qq_33945246/article/details/91040517

https://blog.csdn.net/Jae_Wang/article/details/80526216

2.hashmap 

知識點居多,先留個坑以後再填