Although there are inbuilt method in the commons.lang library using which we can remove the element in array with just a single statement , we will first approach this using a custom method to see how this is done internally.
1. Removing array element with custom method.
public class ArrayRemove { public static void main(String args[]) { int[] myIntArray = { 12, 13, 14, 15, 16 }; System.out.println("Original Array " + Arrays.toString(myIntArray)); int index = findIndexOf(myIntArray, 14); int[] newIntArray = removeElement(myIntArray, index); System.out.println("New Array " + Arrays.toString(newIntArray)); } public static int[] removeElement(int[] intArray, int index) { int[] myNewArray = new int[intArray.length - 1]; for (int i = 0; i < index; i++) { myNewArray[i] = intArray[i]; } for (int i = index + 1; i < intArray.length; i++) { myNewArray[i - 1] = intArray[i]; } return myNewArray; } public static int findIndexOf(int[] intArray, int element) { for (int i = 0; i < intArray.length; i++) { if (intArray[i] == element) return i; } return -1; } }
2. Using Commons lang utility method.
We can solve this using ArrayUtils API provided under apache commons lang package.import java.util.Arrays; import org.apache.commons.lang3.ArrayUtils; public class ArrayRemove1 { public static void main(String args[]){ int[] myIntArray = { 12, 13, 14, 15, 16 }; System.out.println("Original Array " + Arrays.toString(myIntArray)); int[] newArray = ArrayUtils.removeElement(myIntArray, 14); System.out.println("New Array " + Arrays.toString(newArray)); } }