Java ArrayList remove(Object o) method with example


Warning: mysqli_query(): (HY000/1030): Got error 28 from storage engine in /home/ehowtono/public_html/wp-includes/wp-db.php on line 2030

Java ArrayList remove(Object o) method 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 i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Method Signature

public boolean remove(Object o)

Specified by:

remove method in interface Collection

Specified by:

remove method in interface List

Overrides

Overrides:remove method in class AbstractCollection

Parameters:

o – element to be removed from this list, if present

Returns:

true if this list contained the specified element

ArrayList remove(Object o) method Implementation

 public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }
 /*
     * Private remove method that skips bounds checking and does not
     * return the value removed.
     */
    private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work
    }

Java ArrayList remove(Object o) method example

package com.ehowtonow.java.util.arraylist;

import java.util.ArrayList;

public class RemoveExample1 {

	public static void main(String[] args) {

		// create empty array list with Integer type
		ArrayList<Integer> numericList = new ArrayList<Integer>();

		// add() method to add Integer element in the list
		numericList.add(10);
		numericList.add(15);
		numericList.add(20);
		numericList.add(25);
		numericList.add(30);

		// print the values of list
		for (Integer numbers : numericList) {
			System.out.println(" Numbers : " + numbers);
		}

		numericList.remove(1);

		System.out.println("List After value removed : ");
		for (Integer numbers : numericList) {
			System.out.println(" Numbers : " + numbers);
		}

		// create empty array list with String type
		ArrayList<String> stringList = new ArrayList<String>();

		// add() method to add String element in the list
		stringList.add("Welcome");
		stringList.add("To");
		stringList.add("Java");
		stringList.add("ArrayList");

		// print the values of list
		for (String stringValues : stringList) {
			System.out.println("Strings : " + stringValues);
		}

		// removes by index 2 which remove Java
		stringList.remove(2);
		stringList.remove("ArrayList");

		// Again trying to remove Java
		stringList.remove("Java");

		// trying to remove java which is not exist
		stringList.remove("java");

		System.out.println("List After value removed : ");
		for (String stringValues : stringList) {
			System.out.println("Strings : " + stringValues);
		}
		// We cant remove numeric list based on value it will point to index
		numericList.remove(10);

	}

}

Output

Numbers : 10
Numbers : 15
Numbers : 20
Numbers : 25
Numbers : 30
List After value removed :
Numbers : 10
Numbers : 20
Numbers : 25
Numbers : 30
Strings : Welcome
Strings : To
Strings : Java
Strings : ArrayList
List After value removed :
Strings : Welcome
Strings : To
Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 10, Size: 4
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.remove(ArrayList.java:496)
at com.ehowtonow.java.util.arraylist.RemoveExample1.main(RemoveExample1.java:60)


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. F&O Stocks Open = High Screener
  2. Thirukural – Ninaindhavarpulampal – Kural 1203
  3. Thirukural – Kannottam – Kural 575
  4. Thirukural – Pullarivaanmai – Kural 849
  5. TNPSC Chemist In The TN Industries Subordinate Service 2015 Answer Key PDF
  6. TNPSC JUNIOR ANALYST, JUNIOR CHEMIST, CHEMIST AND ARCHAEOLOGICAL CHEMIST 2018 CHEMICAL TECHNOLOGY Question and Answer PDF
  7. TNPSC Study Materials Indian Economy English 4
  8. Java – LongUnaryOperator Interface
  9. TNPSC Study Materials History Tamil 32
  10. TNPSC Curator In Museum Department – Sanskrit – 2019 Question and Answer PDF
  11. Thirukural – Azhukkaaraamai – Kural 161 – 170
  12. Thirukural – Alararivuruththal – Kural 1147
  13. Sending Simple Email Using Java Mail API
  14. TNPSC Research Assistant In Evaluation And Applied Research Department 2016 Revised Question And Answers PDF
  15. TNPSC Study Materials Indian Polity Tamil 45

Be the first to comment

Leave a Reply