I have to create a method that takes in an array and value as arguments and returns a new array with the specified value removed.
Here's my attempt:
public static int[] remove(int[] nums,int value){
int[] after = new int[nums.length-1];
for (int i = 0; i < nums.length; i++) {
if (!(nums[i] == value)) {
after[i] = nums[i];
}
}
nums = after;
return nums;
}
The code throws
ArrayIndexOutOfBoundsException
bur I don't know why.
valueis not the last element in thenumsarray you'll get aArrayIndexOutOfBoundsExceptionsinceafterlength is 1 less thennumlength andigoes as high asnums.length - 1.