Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

In this quick tutorial, we will learn about the various ways in which we can remove an element from an array in Java using the Apache Commons Lang library.

2. Maven

Let's add the commons-lang3 dependency to our project's pom.xml file:


3. Removing an Element

Before we get started, let's look at what happens when we remove an element from an array without using the ArrayUtils class from the Apache Commons Lang library.

Given the array below, let's remove an element at index 2:

Array simple

A simple way of doing this would be to replace the value stored at index 2 with the value stored at index 3 until we reach the end of the array:

Array rm

Notice that by removing the element in the above manner, the size of the array would remain the same and the value stored at the last index would be empty. Since arrays have a fixed memory size allocated during initialization, removing an element does not adjust the size of the array.

Now let's look at the array representation when removing an element using the remove method from ArrayUtils class from Apache Commons Lang:

Array rm common

As we can see, the array size here is adjusted to 5 after the element is removed. The remove method creates a brand new array and copies all the values except for the value being removed.

The ArrayUtils class provides two ways of removing an element from an array. Let's look at these next.

4. Using Index as Input

The first way we can remove an element is by its index with ArrayUtils#remove:

public int[] removeAnElementWithAGivenIndex(int[] array, int index) {
      return ArrayUtils.remove(array, index);

Another variation is the removeAll method, which we can use to remove multiple elements from an array, given their indices:

public int[] removeAllElementsWithGivenIndices(int[] array, int... indices) {
    return ArrayUtils.removeAll(array, indices);

5. Using Element as Input

Or, let's say we don't know the index of what we are removing. In that case, we can provide the element to remove using ArrayUtils#removeElement:

public int[] removeFirstOccurrenceOfGivenElement(int[] array, int element) {
    return ArrayUtils.removeElement(array, element);

Here's another useful variation of this method ArrayUtils#removeElements, in case there is more than one element that we would like to remove:

public int[] removeAllGivenElements(int[] array, int... elements) {
    return ArrayUtils.removeElements(array, elements);

Sometimes, we would want to remove all occurrences of a given element. We can do so by using ArrayUtils#removeAllOccurences:

public int[] removeAllOccurrencesOfAGivenElement(int[] array, int element) {
    return ArrayUtils.removeAllOccurences(array, element);

6. Conclusion

In this article, we looked at the various ways of removing an element/elements from an array using the Apache Commons Lang library.

To learn more about the edge cases, please check out the source code for this tutorial and the relevant unit tests available on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Comments are closed on this article!