Retirez un élément d'un tableau en 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));
}
}
David Zlatin