For-each vs Iterator. Which will be the better option

后端 未结 8 1330
悲&欢浪女
悲&欢浪女 2020-12-02 09:14

Consider the following scenario.

List list = new ArrayList<>();

Now I added the String values for this li

8条回答
  •  情歌与酒
    2020-12-02 09:49

    If you want to replace items in your List, I would go old school with a for loop

    for (int nIndex=0; nIndex < list.size(); nIndex++) {
      Obj obj = (Obj) list.get(nIndex);
    
      // update list item
      list.set(nIndex, obj2);
    }
    

提交回复
热议问题