Write a Java program to remove the fifth element from a array list


The program starts by creating an ArrayList of String type named list_Str and adding nine elements to it using the add() method of ArrayList. The elements added to the list are "Black", "White", "Orange", "Purple", "Green", "Yellow", "Red", "Blue", and "Pink". Then, the program sorts the elements of the list using the sort() method of the Collections class. The sorted list is printed using the println() method of System.out object.

After that, the program removes the fifth element from the list using the remove() method of ArrayList with index 5. Then, the program sorts the elements of the list again using the sort() method of the Collections class. The updated list is printed using the println() method of System.out object.

This is because the sort() method of the Collections class is used to sort the elements of the list in alphabetical order, and the remove() method of ArrayList is used to remove the fifth element of the list, which is "Pink". Finally, the updated list is sorted again and printed using the println() method of System.out object.

Source Code

import java.util.*;
public class Remove_FifthElement
{
	public static void main(String[] args)
	{
		List<String> list_Str = new ArrayList<String>();
		list_Str.add("Black");
		list_Str.add("White");    
		list_Str.add("Orange");
		list_Str.add("Purple");
		list_Str.add("Green");
		list_Str.add("Yellow");
		list_Str.add("Red");
		list_Str.add("Blue");
		list_Str.add("Pink");
		Collections.sort(list_Str);
		System.out.println("Before Removing : "+list_Str);
		list_Str.remove(5);
		Collections.sort(list_Str);
		System.out.println("\nAfter Removing : "+list_Str);
	}
}

Output

Before Removing : [Black, Blue, Green, Orange, Pink, Purple, Red, White, Yellow]

After Removing : [Black, Blue, Green, Orange, Pink, Red, White, Yellow]

Example Programs