Deletes the specified element method from the array in java

  • 2020-05-30 20:03:20
  • OfStack

So how do you do that in java, if you're not familiar with this algorithm you can look at an example of java removing a specified element from an array.

In api of java, there is no way to delete elements from an array. Although the array is one object, there is no way to provide add(), remove(), or find elements. That's why things like ArrayList and HashSet are popular.

However, thanks to Apache Commons Utils, we can use the library's ArrayUtils class to easily delete elements from an array. One thing to note, though, is that arrays are fixed in size, which means that when we remove elements, we don't reduce the size of the array.

So, we can only create one new array, and then copy the remaining elements into the new array using the System.arrayCopy () method. For an array of objects, we can also convert the array to List, then use the methods provided by List to delete the objects, and then convert List to an array.

To avoid trouble, we use the second method:

We use the ArrayUtils class in the Apache commons library to delete the elements we specify by index.

Apache commons lang3 download address:
http://commons.apache.org/proper/commons-lang/download_lang.cgi

After downloading, import jar.


import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
/**
 *
 * Java program to show how to remove element from Array in Java
 * This program shows How to use Apache Commons ArrayUtils to delete
 * elements from primitive array.
 *
 */
public class RemoveObjectFromArray{
  public static void main(String args[]) {
         
    //let's create an array for demonstration purpose
    int[] test = new int[] { 101, 102, 103, 104, 105};
   
    System.out.println("Original Array : size : " test.length );
    System.out.println("Contents : " Arrays.toString(test));
   
    //let's remove or delete an element from Array using Apache Commons ArrayUtils
    test = ArrayUtils.remove(test, 2); //removing element at index 2
   
    //Size of array must be 1 less than original array after deleting an element
    System.out.println("Size of array after removing an element : " test.length);
    System.out.println("Content of Array after removing an object : "
              Arrays.toString(test));
   
  }
 
}
Output:
Original Array : size : 5
Contents : [101, 102, 103, 104, 105]
Size of array after removing an element : 4
Content of Array after removing an object : [101, 102, 104, 105]

Of course, there are other ways, but using existing libraries or java api is faster.

Let's look at ArrayUtils.remove (int[] array, int index)

Method source code:


 public static int[] remove(int[] array, int index) {
    return (int[])((int[])remove((Object)array, index));
  }

In jump to remove((Object)array, index)), source code:


private static Object remove(Object array, int index) {
    int length = getLength(array);
    if(index >= 0 && index < length) {
      Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
      System.arraycopy(array, 0, result, 0, index);
      if(index < length - 1) {
        System.arraycopy(array, index 1, result, index, length - index - 1);
      }
      return result;
    } else {
      throw new IndexOutOfBoundsException("Index: " index ", Length: " length);
    }
  }

Now you see how ArrayUtils works by deleting elements from an array. You still have to use two arrays, and then, using the System.arraycopy () method, copy everything except the element you want to delete into the new array, and then return the new array.


Related articles: