Remove Element From Array and Then Shift Other Elements in Java

Rupam Yadav Jan 30, 2023 May 14, 2021
  1. Use the for Loop to Remove Element From Array and Shift in Java
  2. Use System.arraycopy() to Remove Element From Array and Shift in Java
  3. Use ArrayList to Remove Element From Array and Shift in Java
Remove Element From Array and Then Shift Other Elements in Java

Array in Java is a fixed-size collection of elements of the same type. Java provides various methods to perform different manipulations related to an array.

Use the for Loop to Remove Element From Array and Shift in Java

In this approach, we use multiple for loops to loop over all elements of the arr and check for the element that we want to remove. When the element to be deleted is found, we create a new Array newArr with the size arr.length-1. We run another loop to copy all the elements before the index at which elementToBeDeleted is.

After this, we run another loop to copy the remaining elements of the array after the elementToBeDeleted, leaving the one we have to delete.

import java.util.Arrays;

public class Test {
    public static void main(String[] args){
        int[] arr = {3,1,6,5,2,8,4};
        int[] newArr = null;
        int elementToBeDeleted = 5;
        System.out.println("Original Array is: "+Arrays.toString(arr));

        for (int i = 0; i < arr.length-1; i++) {
            if(arr[i] == elementToBeDeleted){
                newArr = new int[arr.length - 1];
                for(int index = 0; index < i; index++){
                    newArr[index] = arr[index];
                }
                for(int j = i; j < arr.length - 1; j++){
                    newArr[j] = arr[j+1];
                }
                break;
            }
        }
        System.out.println("New Array after deleting element = "+elementToBeDeleted+" and shifting: "+ Arrays.toString(newArr));
    }
}

Output:

Original Array is: [3, 1, 6, 5, 2, 8, 4]
New Array after deleting element = 5 and shifting: [3, 1, 6, 2, 8, 4]

Use System.arraycopy() to Remove Element From Array and Shift in Java

The System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) copies source array to destination array, starting the copy action from the position of the source to the position of the destination, till the given length.

Here, we pass our source array arr to a method removeElement which runs two System.copy() methods. We create arrDestination with a length of 1 less than the length of arr as one element is deleted.

First, we copy the elements before the index 2 as we have to delete 6 from arr. Next, get the remaining elements that exist after the index of the element we intended to delete.

import java.util.Arrays;
public class Test {
    public static void main(String[] args){
        int[] arr = {3,1,6,5,2,8,4};
        removeElement(arr, 2);

    }
    public static void removeElement( int [] arr, int index ){
        int[] arrDestination = new int[arr.length - 1];
        int remainingElements = arr.length - ( index + 1 );
        System.arraycopy(arr, 0, arrDestination, 0, index);
        System.arraycopy(arr, index + 1, arrDestination, index, remainingElements);
        System.out.println("Elements -- "  + Arrays.toString(arrDestination));
    }
}

Output:

Elements -- [3, 1, 5, 2, 8, 4]

Use ArrayList to Remove Element From Array and Shift in Java

We can convert an Array to an ArrayList to remove an element from the array, and the shuffling would be taken care of by the ArrayList itself.

In the code below, we create an Integer object array arr. We then pass the array and the index of the element to be deleted to the method removeElementUsingCollection, which manipulates the array and returns an object array.

The passed array is converted into an ArrayList tempList and calling remove() method removes the element at the specified index from the list. It returns the list converted back to an array.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
    public static void main(String[] args) {
        Integer[] arr = {3, 1, 6, 5, 2, 8, 4};
        int elementToBeDeleted = 2;

        System.out.println("Original Array " + Arrays.toString(arr));
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == elementToBeDeleted) {
                arr = removeElementUsingCollection(arr, i);
                break;
            }
        }
        System.out.println("Array after removing element : "+elementToBeDeleted+ " -- ");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(" " + arr[i]);
        }
    }

    public static Integer[] removeElementUsingCollection( Integer[] arr, int index ){
        List<Integer> tempList = new ArrayList<Integer>(Arrays.asList(arr));
        tempList.remove(index);
        return tempList.toArray(new Integer[0]);
    }
}

Output:

Original Array [3, 1, 6, 5, 2, 8, 4]
Array after removing element : 2 -- 
 3 1 6 5 8 4
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Array