Removing an Item from ArrayList in Java

import java.util.ArrayList;
import java.util.Iterator;

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

		// Creating an ArrayList of generic type
		ArrayList<String> arrList = new ArrayList<String>();
		/*
		 * Arraylist is a growable array, so initialize size would be 0 and it
		 * will get increases with addition of items in it
		 */
		System.out.println("Size of the empty arraylist is :" + arrList.size());

		/*
		 * Adding/Apending an element to the end of this list
		 */

		arrList.add("First Item");
		/*
		 * Inserting the specified element at the specified position in this
		 * list.
		 */
		arrList.add(1, "Second Item");
		System.out.println("Before removing the element from list size is :"
				+ arrList.size());

		/*
		 * Removing the element at the specified position in given list. Below
		 * code will remove the 0th element from the list
		 */
		System.out
				.println("Element removed from list is :" + arrList.remove(0));

		/*
		 * check now the size of list
		 */

		System.out.println("After removing the element from list size is :"
				+ arrList.size());

		/*
		 * now search for the first element if it is present in list or not
		 */

		System.out.println("Given object is present in list(true/false) :"
				+ arrList.contains("First Item"));

		/*
		 * Removing the given element from the list, if it is present. If the
		 * list does not contain the element, list will be unchanged. If list
		 * contains more then one occurance of given element only first
		 * occurance will be removed.
		 */
		System.out
				.println("Given element has been removed from list(true/false) :"
						+ arrList.remove("Second Item"));

		/*
		 * checking if removed element is present in list or not it should
		 * return false
		 */
		System.out.println("Given object is present in list(true/false) :"
				+ arrList.contains("Second Item"));

		/*
		 * Now removing the collection of Object from the list
		 */

		// again populating the list
		arrList.add("First Item");
		arrList.add("Second Item");
		arrList.add("Third Item");
		arrList.add("Fourth Item");

		// Creating an ArrayList to search for
		ArrayList<String> arrListToRemove = new ArrayList<String>();
		// populating the above list with Objects to search in given list

		arrListToRemove.add("First Item");
		arrListToRemove.add("Second Item");
		arrListToRemove.add("Third Item");

		System.out
				.println("Objects in Collection has been removed successfully(true/false)"
						+ arrList.removeAll(arrListToRemove));

		// again populating the list
		arrList.add("First Item");
		arrList.add("Second Item");
		arrList.add("Third Item");
		arrList.add("Fourth Item");

		/*
		 * populating collection with elements to remove from the list Note here
		 * we have added one item "Fourth Item" which is not there in list
		 * removeall will remove all the element which is there in the list
		 */
		arrListToRemove.add("First Item");
		arrListToRemove.add("Second Item");
		arrListToRemove.add("Fourth Item");

		System.out
				.println("Objects in Collection has been removed successfully(true/false)"
						+ arrList.removeAll(arrListToRemove));
	}
}

 

By | 2017-07-22T23:05:35+00:00 April 21st, 2015|Code Base|0 Comments

About the Author:

Leave A Comment