Core Java

Remove element from an Array Java Example

Photo of YatinYatinOctober 3rd, 2019Last Updated: July 6th, 2022
1 294 3 minutes read

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 fordeleting 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
packagecom.java;
 
publicclassOldWayOfDoingThings {
 
    publicstaticvoidmain(String[] args) {
 
        // Initializing a sample array.
        int[] arr =newint[] {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 =newint[arr.length -1];
 
        System.out.println("Original array values: ");
        for(inti=0; i<arr.length; i++) {
            System.out.print(arr[i] +" ");
        }
 
        System.out.println("\n");
 
        // Removing the element from index=2 -> (value=15).
        intremoveEleFromIndex =2;
        // Copying the elements to the new array except the index from the original array.
        for(intj=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(intl=0; l<newArr.length; l++) {
            System.out.print(newArr[l] +" ");
        }
    }
}

If everything goes well, the element present atindex=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 theJava 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 ofcommon-lang3 to perform a remove operation on an array. To use this approach ensure having thecommons-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
packagecom.java;
 
importjava.util.Arrays;
 
importorg.apache.commons.lang3.ArrayUtils;
 
publicclassNewWayOfDoingThings {
 
    publicstaticvoidmain(String[] args) {
 
        // Initializing a sample array.
        int[] arr =newint[] {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 atindex=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 theDownloads 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
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of YatinYatinOctober 3rd, 2019Last Updated: July 6th, 2022
1 294 3 minutes read
Photo of Yatin

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).

Related Articles

Bipartite Graph

Java not equal Example

January 17th, 2020
Bipartite Graph

Java API Tutorial

October 26th, 2020
Bipartite Graph

Java Struct Example

January 8th, 2020
Bipartite Graph

Java Node Example

November 20th, 2019
Bipartite Graph

Java Swing MVC Example

January 26th, 2016
Bipartite Graph

How to call a method in Java

December 26th, 2019
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.