简体   繁体   中英

Arraylist Remove() does not work

public static ArrayList<String> remove(ArrayList<String> list, int a) {

    for(int i = 0; i < list.size(); i++){
            list.remove(i);
    }
    return list;
}

Why doesn't this code remove every element in my array? It seems to skip some. When I print the arraylist at the end it should be blank but it prints it with some elements still in there.

Edit (more code):

public static void main(String [] args){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter some a list of words:");
    String line = scan.nextLine();
    String[] words = line.split(" +");
    ArrayList<String> list  = new ArrayList<String>();
    for(int i=0; i<words.length; i++){
        list.add(words[i]);
    }

    System.out.println("Remove words less than how many characters?");
    int a = scan.nextInt();

    remove(list,a);
    System.out.println(list);
}

When you remove the i th element, the i+1 th element becomes the i th element. And since you increment i in each iteration, you skip half of the elements in the list.

This loop would remove all the elements :

for(int i = 0; i < list.size();) {
    list.remove(i);
}

Javadoc :

public E remove(int index)

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices) .

Now, if you wish to iterate over all the elements of the list while removing just some of them, you can do the following :

for(int i = 0; i < list.size(); i++) {
    if (someCondition) {
        list.remove(i);
        i--;
    }
}

This will make sure you don't skip any element.

When you remove the i th element, all other elements are shifted down. Then you increment i anyway, which skips the second element that was originally at i == 1 .

You can remove the elements in a backwards order to avoid this. You can also just call clear() on the ArrayList .

Imagine you have 10 items.

First iteration

 list.remove(0); The element at the beginning gets removed

Second iteration

 list.remove(1); What is now the second element gets removed. But the element who is in position 0 is not removed!

Third iteration

  list.remove(2); What is now the third element gets removed. But the elements now in position 0 and 1 are not changed!

User removeAll or, if you want to iterate, always do remove(0)

Basically, between each remove, the remaining elements get new positions, or new indexes.

In practice, you really should use

list.clear()

for this particular usage.

Why does this not remove every element in my array?

After removing each element in ArrayList other elements after it are shifted left so for elements like

a,b,c,d

remove(0) will produce

b,c,d

Now when you call remove(1) this removes element at position 1 which is

b,c,d
  ^-this one

so you end up with

b,d

Now size() of this list is equal 2 , index was increased to 2 so your loop ends because i < list.size() condition is no longer true.


When i print the arraylist at the end it should be blank but it prints it with some elements still in there.

If you want to remove all elements in list just use yourList.clear() method.
If you want to remove them manually always use remove(0) in each forward iteration and change your condition to yourList.size()>0 , but this will be very inefficient way because of shifting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM