Core Java

Remove element from an Array Java Example

Hello readers, in this tutorial, we will learn two different ways to remove an element from an array.So lets talk about java array remove operation.

You can watch the following video and learn how to use arrays in Java:

Java Array Example How to use Arrays in Java – Video

1. Introduction

In Java programming, Arrays represent an index-based object that consists of the same data type elements. The array elements are stored in a common memory location and can store only a fixed number of elements. Arrays offer,

  • Code Optimization
  • Random Access
Remove element from an Array Java - Pictorial representation of an Array
Fig. 1: Pictorial representation of an Array

To start with the tutorial, we are hoping that users at present have their preferred Ide installed on their machines. For easy usage, I am using Eclipse Ide on a Windows operating system.

2. Remove element from an Array Java Example

In this example, I’ll demonstrate two ways for deleting an element from an array. For a better understanding, developers can execute the below code in Eclipse Ide.

2.1 Old Style of Working

Approach 1 talks about a mature way of deleting an element from an array. Let us understand this with the help of a simple code snippet.

Example 1

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.java;
 
public class OldWayOfDoingThings {
 
    public static void main(String[] args) {
 
        // Initializing a sample array.
        int[] arr = new int[] { 5, 10, 15, 20, 25, 30 };
 
        // New array for copying elements from old array.
        // For simplicity we are decrementing the old array length by 1 and assigning it to the new array.
        int[] newArr = new int[arr.length - 1];
 
        System.out.println("Original array values: ");
        for(int i=0; i<arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
 
        System.out.println("\n");
 
        // Removing the element from index=2 -> (value=15).
        int removeEleFromIndex = 2;
        // Copying the elements to the new array except the index from the original array.
        for(int j=0, k=0; j<arr.length; j++) {
            if(j == removeEleFromIndex)
                continue;
 
            newArr[k++] = arr[j];
        }
 
        System.out.println("After removing an element from the given index: ");
        for (int l=0; l<newArr.length; l++) {
            System.out.print(newArr[l] + " ");
        }
    }
}

If everything goes well, the element present at index=2 will be removed from the specified array.To find out more about the best way to copy an array for each possible case you can check the Java Copy Array Example

Output

1
2
3
4
5
Original array values:
5 10 15 20 25 30
 
After removing an element from the given index:
5 10 20 25 30

2.2 New Style of Working

Approach 2 talks about the use of common-lang3 to perform a remove operation on an array. To use this approach ensure having the commons-lang3 dependency in your project. Let us understand this with the help of a simple code snippet.

Example 2

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.java;
 
import java.util.Arrays;
 
import org.apache.commons.lang3.ArrayUtils;
 
public class NewWayOfDoingThings {
 
    public static void main(String[] args) {
 
        // Initializing a sample array.
        int[] arr = new int[] { 35, 40, 45, 50, 55, 60 };
 
        System.out.println("Original array values: "+ Arrays.toString(arr));
 
        System.out.println("\n");
 
        // Removing the element from index=2 -> (value=45).
        // Where "2" refer to the index position.
        arr = ArrayUtils.remove(arr, 2);
 
        System.out.println("After removing an element from the given index: "+ Arrays.toString(arr));
    }
}

If everything goes well, the element present at index=2 will be removed from the specified array.

Output

1
2
3
Original array values: [35, 40, 45, 50, 55, 60]
 
After removing an element from the given index: [35, 40, 50, 55, 60]

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

3. Conclusion

In this tutorial, we learned how to remove an element from a given array. Developers can download the sample application as an Eclipse project in the Downloads section.

4. Download the Eclipse Project

This was an example of removing an element from a given array.

Download
You can download the full source code of this example here: Remove element from an Array Java Example

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
nskmda
nskmda
4 years ago

r u kidding me?

Stream.of().filter().toArray()

is not working any longer?

Back to top button