ArrayList.remove is not working in a loop

后端 未结 7 1623
南旧
南旧 2020-11-29 09:46

I have following code-

import java.util.ArrayList;

public class ArrayListExp{
    public static void main (String[] args){

        ArrayList          


        
7条回答
  •  孤城傲影
    2020-11-29 09:58

    You can use name.removeAll(Arrays.asList("Meg")); to remove all "Meg"

    Your complete code would be

    for ( int i = 0;  i < name.size(); i++){
        String oldName = name.get(i);
        if(oldName.equals("Meg"))
        {
           name.removeAll(Arrays.asList("Meg"));
        }
    }
    

提交回复
热议问题