Pritish
Pritish

Reputation: 1368

What does remove() method in ArrayList datatype exactly returns

I am using list.remove() method in my code. I was trying to pass the returned value as a param to some function as a string so that it does both the job of returning as well as removing the object. When I print it in for loop it displays only two elements when it should print 4 . Why is it happening so ?

ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        for(int i=0;i<list.size();i++) {
            Log.d("Removed+++++++++++","++++"+list.remove(i));
           // queue.remove();
        }

D/Removed+++++++++++: ++++a
D/Removed+++++++++++:++++c

Why does it only print a and c when it should print a,b,c,d ?

Upvotes: 1

Views: 888

Answers (4)

ArifMustafa
ArifMustafa

Reputation: 4965

Actually, after removing value from ArrayList object list inside loop, the size of the list decreases and your for loop index value is increasing by 1, so after deleting two values, your list size remains two and the i value is 2 so the condition failed and loop got terminated.

Try the code below:

ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");

while(list.size() > 0){
    String removingValue = list.get(0);
    if(list.remove(0) != null)
        System.out.println("After removing " + removingValue + " list size is : " + list.size());
}

and the output is the following:

After removing a list size is : 3
After removing b list size is : 2
After removing c list size is : 1
After removing d list size is : 0

Hope your logic cleared.

Upvotes: 0

Przemysław Moskal
Przemysław Moskal

Reputation: 3609

As Java API documentation states:

E remove(int index) - removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

You may want to change your loop to while-loop if you don't want to increment the index of element you want to remove in next loop as it seems unnecessary in your example (it caused problem in your for-loop, you didn't decrement this index inside the loop again).

ArrayList<String> list = new ArrayList<>(); list.add("a");
list.add("b");
list.add("c");
list.add("d");
while (list.size() > 0) {
    Log.d("Removed+++++++++++","++++" + list.remove(0)); // queue.remove();
}

Upvotes: 3

Alex Mi
Alex Mi

Reputation: 1459

You should keep in mind that there are two remove() methods in the ArrayList class:

/**
     * Removes the element at the specified position in this list.
     * Shifts any subsequent elements to the left (subtracts one from their
     * indices).
     *
     * @param index the index of the element to be removed
     * @return the element that was removed from the list
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public E remove(int index) {

the other remove method looks as follows:

/**
     * Removes the first occurrence of the specified element from this list,
     * if it is present.  If the list does not contain the element, it is
     * unchanged.  More formally, removes the element with the lowest index
     * <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
     * (if such an element exists).  Returns <tt>true</tt> if this list
     * contained the specified element (or equivalently, if this list
     * changed as a result of the call).
     *
     * @param o element to be removed from this list, if present
     * @return <tt>true</tt> if this list contained the specified element
     */
    public boolean remove(Object o) {

Upvotes: 0

Eran
Eran

Reputation: 393856

It returns the element that was removed, as stated in the Javadoc:

E java.util.List.remove(int index)

Returns:
the element previously at the specified position

It only prints "a" and "c" because it only removed "a" and "c".

When removing elements from an ArrayList, the indices of the following elements are decremented. You have to account for that if you wish your loop to remove all elements. Otherwise you'll skip half of the elements.

for(int i=0;i<list.size();i++) {
    Log.d("Removed+++++++++++","++++"+list.remove(i));
    i--;
}

Upvotes: 7

Related Questions