Java

How to delete array elements in java

Arrays are the core concept of any programming language including java. In Java, a predefined class Array provides numerous methods to perform different tasks or operations on the arrays such as sorting, traversing, searching, etc. But you would be surprised to know that the Array class didn’t provide any method to delete an array element. So, how to delete array elements in Java?

Well! We can adopt various approaches to delete the array elements in java such as using multiple arrays, using arraylist, etc.

This article will assist us to delete array elements and to do so, it will explain the below-listed methods:

So, let’s begin!

How to Delete Array Element Using remove() Method

It is a predefined method of the ArrayList class that can be used to delete the array’s elements. To delete the array’s element using remove() method, first, we have to convert the array into an arraylist. Once the array is converted into an arraylist then we can utilize the remove() method of the Arraylist class to delete the array elements.

Example

Let’s consider the below code snippet to understand how to use remove() method of ArrayList class with arrays to delete some specific element of an array:

public class DeleteArrayElement {

public static int[] deleteElement(int[] originalArray, int deleteIndex) {

if (originalArray == null || deleteIndex < 0 || deleteIndex >=

originalArray.length)

{

return originalArray;

}

List<Integer> elementList = IntStream.of(originalArray).boxed() .collect(Collectors.toList());

elementList.remove(deleteIndex);

return elementList.stream().mapToInt(Integer::intValue).toArray();

}

public static void main(String[] args) {

int[] originalArray = {30, 25, 50, 80, 20, 60};

System.out.println("Original Array: " + Arrays.toString(originalArray));

Scanner input = new Scanner(System.in);

System.out.println("Enter the index you want to delete");

int deleteIndex = input.nextInt();

originalArray = deleteElement(originalArray, deleteIndex);

System.out.println("Modified Array: " + Arrays.toString(originalArray));

}

In this Java program we performed the following tasks:

  • Created an integer array named “originalArray” and assigned six elements to that array.
  • Utilized the Scanner class to get the array index to be deleted from the user.
  • Created a method named deleteElement() which will return the originalArray if the array is empty or if the array index is out of range.
  • Created an ArrayList named “elementList” that holds the elements of the “originalArray”.
  • Next, we utilized the remove() method to delete the given index.
  • Afterward, we utilized the mapToInt() and toArray() methods to create a new array.

As a result we will get the following output:

The output shows that the remove() method succeeded in deleting the specified array element from the original array.

How to Use Multiple Arrays to Delete Array Elements

In java, we can utilize multiple arrays to delete an array element. It is one of the simplest approaches to delete an array element which says:

  • Find the targeted element at the specific index.
  • Delete that element.
  • Copy the remaining elements to the new array.

In this way, the newly created array will have a size one less than the original array.

Example

Let’s consider the following code snippet for the profound understanding of this concept:

package deletearrayelement;

import java.util.Arrays;

import java.util.Scanner;

public class DeleteArrayElement {

public static int[] deleteElement(int[] originalArray, int deleteIndex) {

if (originalArray == null || deleteIndex < 0 || deleteIndex >= originalArray.length)

{

return originalArray;

}

int[] secondArray = new int[originalArray.length - 1];

for (int i = 0, j = 0; i < originalArray.length; i++) {

if (i == deleteIndex) {

continue;

}

secondArray[j++] = originalArray[i];

}

return secondArray;

}

public static void main(String[] args) {

int[] originalArray = {30, 25, 50, 80, 20, 60};

System.out.println("Original Array: "+ Arrays.toString(originalArray));

Scanner input = new Scanner(System.in);

System.out.println("Enter the index you want to delete");

int deleteIndex = input.nextInt();

originalArray = deleteElement(originalArray, deleteIndex);

System.out.println("Modified Array: " + Arrays.toString(originalArray));

}

}

The above code snippet performed the following functionalities:

  • Created an integer array named “originalArray”, utilized the Scanner class to get the array index to be deleted, and created a method named deleteElement() to return the originalArray in case the array is empty or the array index is out of range.
  • Next, we created another array named “secondArray” whose length is one less than the original array and copied all the elements of the “originalArray” (except the targeted element/index) to the “secondArray”.
  • We utilized the for-loop to compare the current index with the index to be deleted. If the current index is equal to the targeted index then that index will be skipped and the loop will be moved to the next index.
  • In this way, the specified element will be deleted from the given array.

Following will be the output for the above code snippet:

The user entered “2”, consequently, the above-specified code deleted the element present at the second index (i.e. 50) from the original array.

How to Delete Array Element Using Java8 Streams

We can also use the Java8 Streams to delete the array elements. To do so, first we will convert the array into a stream, then we will create a new filtered/modified array.

Example

Let’s consider the below code block to understand how to delete array elements using Java8 streams:

public class DeleteArrayElement {

public static int[] deleteElement(int[] originalArray, int deleteIndex) {

if (originalArray == null || deleteIndex < 0 || deleteIndex >=

originalArray.length)

{

return originalArray;

}

return IntStream.range(0,originalArray.length).filter(i -> i!= deleteIndex)

.map(i->originalArray[i]).toArray();

}

public static void main(String[] args) {

int[] originalArray = {30, 25, 50, 80, 20, 60};

System.out.println("Original Array: " + Arrays.toString(originalArray));

Scanner input = new Scanner(System.in);

System.out.println("Enter the index you want to delete");

int deleteIndex = input.nextInt();

originalArray = deleteElement(originalArray, deleteIndex);

System.out.println("Modified Array: " + Arrays.toString(originalArray));

}

}

The above code block performed the following tasks:

  • We utilized the IntStream.range() method to convert the array into a stream.
  • Next, we utilized the filter() method to remove the targeted array index.
  • Finally, we utilized the map() and toArray() methods to create a new filtered/modified array.

The complete code and corresponding output will be something like this:

This is how we can use the Java8 Streams to delete the array elements.

Conclusion

In Java, multiple approaches can be used to delete the array elements such as remove() method of ArrayList class, filter() method of Java8 Streams, etc. Moreover, in Java, multiple arrays can be used to delete some specific element of an array. This write-up presented a detailed guide to delete the array elements in java. For better understanding, it explained each method with the help of suitable examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.