Java Program to delete element at the end of Array

Java Program to delete element at the end of Array

In this tutorial, we will be learning to write a java program to delete elements from the end of the array.

There is multiple logic to achieve this. In this tutorial, we will see 2-3 logics to remove the last element from an array using a java program.

Program 1: Using Arrays.copyOf() method to remove the last element

This is a simple program to remove the last element from an array. Here we will be using Arrays.copyOf() method to copy the array element from the original array into a new array having a size one less than the original array.

Also in our program, we are taking the array elements as input from the users.

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Java Program to delete last element from Array");
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr[i] = sc.nextInt();
        }
        arr = Arrays.copyOf(arr, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }
}

Output

Program 2: Using Arrays.copyOfRange() to remove last element from array

In this program, we will be using copyOfRange() method to remove the last element from the array. Here copyOfRange() method will copy the element only in the given range. So here we will be giving the range from starting to till length-1.

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Java Program to delete last element from Array");
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr[i] = sc.nextInt();
        }
        arr = Arrays.copyOfRange(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }
}

Output

Program 3: Using System.arraycopy() method

In the below program, we will be using arraycopy() method to remove the last element from the array.

Syntax of arraycopy() is

arraycopy(Object source_arr, int sourcePos,
                            Object dest_arr, int destPos, int len)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Java Program to delete last element from Array");
        System.out.print("Enter the size of array: ");
        int size = sc.nextInt();
        int arr[] = new int[size];
        for(int i=0; i<size; i++) {
            System.out.print("Please give value for index "+ i +" : ");
            arr[i] = sc.nextInt();
        }
        int[] arrOutput = new int[arr.length - 1];
        System.arraycopy(arr, 0, arrOutput, 0, arr.length - 1);
        System.out.println(Arrays.toString(arrOutput));
    }
}

Output

In this tutorial, we have seen a total of 3 ways to write a program in java to remove the last element at the end of the array. Hope you will be able to understand it.