Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

remove an element from an array in java


import java.util.Arrays;
import java.util.stream.IntStream;
 
class Main
{
    public static int[] remove(int[] a, int index) {
        if (a == null || index < 0 || index >= a.length) {		//lowering the possibilities of an error
            return a;
        }
 	
        else {
          return IntStream.range(0, a.length)
                        .filter(i -> i != index)
                        .map(i -> a[i])
                        .toArray();
        }
    }
 	
    public static void main(String[] args) {		//an example of how to use it
        int[] a = { 1, 2, 3, 4, 5 };
        int index = 2;
 
        a = remove(a, index);
        System.out.println(Arrays.toString(a));
    }
}
Source by www.techiedelight.com #
 
PREVIOUS NEXT
Tagged: #remove #element #array #java
ADD COMMENT
Topic
Name
2+4 =