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.
First you need to find how big the new array is, after you can populate the new array without the value to remove. There are better ways to do this, but this is to get you started.