Using ArrayUtills in java code, you can remove an element from the java array. Below is the code that deletes an element in a separate index (in code "2", which deletes an element value equal to "10".
import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
public class RemoveObjectFromArray{
public static void main(String args[]) {
int[] test = new int[] { 10, 13, 10, 10, 105};
System.out.println("Original Array : size : " + test.length );
System.out.println("Contents : " + Arrays.toString(test));
test = ArrayUtils.remove(test, 2);
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));
} }
It outputs as:
run:
Original Array : size : 5
Contents : [10, 13, 10, 10, 105]
Size of array after removing an element : 4
Content of Array after removing an object : [10, 13, 10, 105]
How can I change the code to get the following output:
run:
Original Array : size : 5
Contents : [10, 13, 10, 10, 105]
Size of array after removing an element : 2
Content of Array after removing an object : [ 13, 105]
source
share