How to add element in List while iterating in java?

前端 未结 5 892
暖寄归人
暖寄归人 2020-12-15 16:25

Say I have a List like:

List list = new ArrayList<>();
list.add(\"a\");
list.add(\"h\");
list.add(\"f\");
list.add(\"s\");
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 16:35

    Just iterate the old-fashion way, because you need explicit index handling:

    List myList = ...
    ...
    int length = myList.size();
    for(int i = 0; i < length; i++) {
       String s = myList.get(i);
       // add items here, if you want to
    }
    

提交回复
热议问题