5
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);
}
5
  • 2
    What is the a parameter for in this method? Please also post the code you're using to test this method and print the results. Commented Oct 7, 2014 at 18:00
  • Because I need to remove only certain elements, i got rid of the rest of the code to avoid confusion. Commented Oct 7, 2014 at 18:02
  • Actually, remove() is working as intended. The method of removing all the elements is not working as intended. Commented Oct 7, 2014 at 18:03
  • Why are you passing a to your remove, what is it for? Commented Oct 7, 2014 at 18:05
  • that's why I use list.remove(i--); when I need this functionality. Commented Oct 7, 2014 at 18:09

5 Answers 5

10

When you remove the ith element, the i+1th element becomes the ith 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.

Sign up to request clarification or add additional context in comments.

Comments

2

When you remove the ith 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.

2 Comments

@Downvoter Please explain how you think this post can be improved.
+1, judging by the OP's comments, iterating backwards is what they are looking for.
1

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)

Comments

1

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.

Comments

-1

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.

1 Comment

@downvoter can you explain what exactly is wrong with this answer? I am not saying you are not right, I am just curious what I did wrong (I like to learn from mistakes).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.