How to delete an element and delete all elements of an ArrayList

In this article, we will discuss an example of ArrayList on how to delete a particular element and later deleting all elements

 

ArrayList :

  • remove(Object) –> to delete a particular element by specifying object name
  • clear() –> to delete all elements present in ArrayList
  • Note: Both above methods inherited from Collection interface

 

Example: To delete an element and delete all elements of an ArrayList

Below ArrayList example depicts

  • Deleting a particular element by specifying object name
  • Deleting all elements or clearing ArrayList

DeleteAndDeleteAllElementsOfArrayList.java

package in.bench.resources.java.collections;

import java.util.ArrayList;

public class DeleteAndDeleteAllElementsOfArrayList {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		ArrayList<String> al = new ArrayList<String>();

		// adding elements to ArrayList object
		al.add("Sundar Pichai");
		al.add("Satya Nadella");  
		al.add("Shiv Nadar");  
		al.add("Shantanu Narayen");
		al.add("Sundar Pichai"); // duplicate object
		al.add("Francisco D’Souza");
		al.add(null); // null is allowed

		System.out.println("Intial size of an ArrayList is : "
				+ al.size());

		// Iterating using for-loop
		System.out.println("\nIterating "
				+ "ArrayList elements using for-loop\n");
		for(int index = 0; index < al.size(); index++) {
			System.out.println(al.get(index));
		}


		// deleting particular element
		al.remove("Shiv Nadar");

		System.out.println("\n\nArrayList size after deleting : "
				+ al.size());

		// Iterating using enhanced for-loop
		System.out.println("\nIterating "
				+ "after deleting particular object\n");
		for(String str : al) {
			System.out.println(str);
		}


		// deleting all elements
		al.clear();

		System.out.println("\n\nArrayList size after clearing : "
				+ al.size());

		// Iterating using enhanced for-loop
		System.out.println("\nIterating after clearing ArrayList\n"
				+ al);
	}
}

Output:

Intial size of an ArrayList is : 7

Iterating ArrayList elements using for-loop

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null


ArrayList size after deleting : 6

Iterating after deleting particular object

Sundar Pichai
Satya Nadella
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null


ArrayList size after clearing : 0

Iterating after clearing ArrayList
[]

From above example, ArrayList

  • allows duplicate elements
  • null object is allowed
  • while iterating insertion-order is maintained

What happens to ArrayList, when duplicate element is deleted ?

  • Deletes 1st occurrence of duplicate element
  • but duplicate elements in ArrayList remain unchanged
  • except changing its index-position
  • i.e.; moving upward direction by one index-position

 

References:

 

Happy Coding !!
Happy Learning !!

How to get sublist from ArrayList
Remove element from ArrayList at specified index position