Java program to remove element from an ArrayList of a specific index

Java program to remove element from an ArrayList of a specific index :

In this program, we will learn how to remove an element of a specific index from an ArrayList. First, we will take ‘n’ number of inputs from the user . Next we will get the index of the number to be removed. To remove an element from a ArrayList, we can use ‘remove(index)’ method. ‘index’ is the index number. If the index is available, we will remove that number , otherwise we will ask the user for a valid input. Following algorithm we are going to use in this example :

Algorithm :

1. Get the count of numbers from the user.
2. Use one ‘for’ loop and read all numbers . Insert all in an arraylist.
3. Print the arraylist.
4. Ask the user , which index number need to be removed.
5. Use one infinite loop. This loop will run till the arraylist becomes empty or if the user enters ‘-1’ as input.
6. After the index is read, always check if it is valid or not. If valid, remove the number from the arraylist at that index.
7. Repeat the while loop again.

Java Program :

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    private static void print(String message) {
        System.out.print(message);
    }

    private static void println(String message) {
        System.out.println(message);
    }

    public static void main(String[] args) throws java.lang.Exception {
        Scanner sc = new Scanner(System.in);

        //initialize an arraylist
        ArrayList numberList = new ArrayList();

        println("How many numbers you want to add ?");

        //read user input total count
        int no = sc.nextInt();

        int userInputNo;

        //read all numbers from
        for (int i = 0; i < no; i++) { print("Enter No " + (i + 1) + " : "); userInputNo = sc.nextInt(); numberList.add(userInputNo); } //print the arraylist println(""); println("You have entered : "); println(numberList.toString()); //ask the user which number want to remove while (true) { if (numberList.size() == 0) { println("Empty arraylist, Exiting..."); break; } println(""); println("Enter index no. you want to remove from this list , -1 to exit : "); int index = sc.nextInt(); if (index == -1) { println("Exiting..."); break; } else if (index >= numberList.size()) {
                println("Please enter a valid index no.");
                continue;
            }
            numberList.remove(index);
            println("");
            println("Arraylist after element on position " + index + " removed : ");
            println(numberList.toString());

        }

    }

}

Sample Output :

How many numbers you want to add ?
3
Enter No 1 : 4
Enter No 2 : 5
Enter No 3 : 6

You have entered : 
[4, 5, 6]

Enter index no. you want to remove from this list , -1 to exit : 
1

Arraylist after element on position 1 removed : 
[4, 6]

Enter index no. you want to remove from this list , -1 to exit : 
0

Arraylist after element on position 0 removed : 
[6]

Enter index no. you want to remove from this list , -1 to exit : 
3
Please enter a valid index no.

Enter index no. you want to remove from this list , -1 to exit : 
0

Arraylist after element on position 0 removed : 
[]
Empty arraylist, Exiting...

Process finished with exit code 0
Sitewide-USD 336x280

Leave a Reply