Write a java program to remove nth numbers element from ArrayList

Write a program that accepts an ArrayList of integers as input and removes every 3rd element and prints the final ArrayList. Suppose the given ArrayList contains 10 elements remove the 3rd, 6th, and 9th elements.

Input and Output Format

  • The first line of the input consists of an integer n, that corresponds to the number of elements to be added in the ArrayList.
  • The next n lines consist of integers that correspond to the elements in the ArrayList.
  • Output consists of an ArrayList of integers.

Sample Input:

6

3

1

11

19

17

19

Sample Output

3

1

19

17

Java program to remove nth numbers element from ArrayList

Let us see the following steps to remove nth number:

  • First, create two empty Array List. And, input the size.
  • Now, add elements to the list using add() method.
  • Call the method find() with the list.
  • Inside the method, create an empty list and check if every 3rd element is divisible by 3 then add it to the newly created list. Repeat this process till the end of the list.
  • And, at last return this list and print.
package com.demo;
 
import java.util.*;
 
public class ArrayListProgram {
 
private static Scanner sc;
 
public static void main(String[] args) {
sc = new Scanner(System.in);
ArrayList<Integer> al = new ArrayList<Integer>();
ArrayList<Integer> al1 = new ArrayList<Integer>();
int n = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n; i++) {
al.add(sc.nextInt());
}
al1 = find(al);
Iterator it = al1.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
 
public static ArrayList<Integer> find(ArrayList<Integer> al) {
ArrayList<Integer> al2 = new ArrayList<Integer>();
 
for (int i = 0; i < al.size(); i++) {
if ((i + 1) % 3 != 0)
al2.add(al.get(i));
}
 
return al2;
}
}

Output

Remove nth element in Array List by Value

Write a program to remove an nth element from an Array List and display elements.

Program to remove an nth element in Array List by Value

Following is the step to remove element by value:

  • Create an empty array list. And, input the size of the list.
  • Add elements to the list using add() method.
  • Now, enter the element that you want to remove. 
  • Then, call the remove(Object) method that will remove elements by value. 
  • At last, print the array list.
package com.company;

import org.omg.PortableInterceptor.INACTIVE;

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

public class ArrayListProgram {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<>();
        int n = sc.nextInt();
        for(int i =0;i<n;i++){
            list.add(sc.nextInt());
        }
        System.out.println("Enter element to remove");
        int element = sc.nextInt();

        list.remove(new Integer(element));

        System.out.println("List after removing "+element);
        System.out.println(list);
    }
}

Output

Remove nth element in Array List by index

Write a program to remove an nth element from an Array List and display elements.

Program to remove an nth element in Array List by index

Following is the step to remove element by index:

  • Create an empty array list. And, input the size of the list.
  • Add elements to the list using add() method.
  • Now, enter the element that you want to remove. 
  • Then, call the remove(int) method that will remove elements by value at the specified index. 
  • At last, print the array list.
package com.company;

import org.omg.PortableInterceptor.INACTIVE;

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

public class ArrayListProgram {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<>();
        int n = sc.nextInt();
        for(int i =0;i<n;i++){
            list.add(sc.nextInt());
        }
        System.out.println("Enter index of element to remove");
        int element = sc.nextInt();

        list.remove(element);

        System.out.println("List after removing index  "+element);
        System.out.println(list);
    }
}

Output

Thus, in this way, we learn how to remove every 3rd element from an array list in Java. Along with removing elements by value & index.