如何使用方法从java中的数组中删除项?

如何使用方法从java中的数组中删除项?,java,arrays,indexoutofboundsexception,Java,Arrays,Indexoutofboundsexception,我必须创建一个方法,该方法接受数组和值作为参数,并返回一个新数组,其中删除了指定的值 以下是我的尝试: 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] = n

我必须创建一个方法,该方法接受数组和值作为参数,并返回一个新数组,其中删除了指定的值

以下是我的尝试:

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;
}
publicstaticint[]remove(int[]nums,int-value){
int[]after=新int[nums.length-1];
对于(int i=0;i
代码抛出

ArrayIndexOutOfBoundsException


但是我不知道为什么。

在填充新数组而不删除值之后,首先需要找到新数组的大小。有更好的方法可以做到这一点,但这是为了让你开始

public static int[] remove (int[] nums, int value)
{
    int[] after;

    //find out the length of the array
    int count = 0;
    for (int num : nums)
    {
        if (num != value)
        {
            count++;
        }
    }

    after = new int[count];

    //add all the elements except the remove value
    int i = 0;
    for (int num : nums)
    {
        if(num != value)
        {
            after[i] = num;
            i++;
        }
    }

    return after;
}

首先,在填充新数组而不删除值之后,您需要找到新数组的大小。有更好的方法可以做到这一点,但这是为了让你开始

public static int[] remove (int[] nums, int value)
{
    int[] after;

    //find out the length of the array
    int count = 0;
    for (int num : nums)
    {
        if (num != value)
        {
            count++;
        }
    }

    after = new int[count];

    //add all the elements except the remove value
    int i = 0;
    for (int num : nums)
    {
        if(num != value)
        {
            after[i] = num;
            i++;
        }
    }

    return after;
}

代码试图将值从原始数组复制到新数组,并在目标值处留下空白或空格(值0)。显示错误
ArrayIndexOutOfBoundsException
,因为您正在使用nums.lenth-1
int[]after=new int[nums.length-1]初始化after
您可以更改该值,或者将目标元素与最后一个元素交换,并复制除最后一个元素之外的整个数组

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)) {
            //changing the target value with the last value of nums array
            nums[i]=nums[nums.length-1];
            nums[nums.length-1]=value;
        }
    }

    //Copying the entire array except the last
    for(int i=0;i<nums.length-1;i++){
        after[i]=nums[i];
    }
    return after;
}
publicstaticint[]remove(int[]nums,int-value){
int[]after=新int[nums.length-1];
对于(int i=0;ifor(int i=0;i代码试图将值从原始数组复制到新数组,留下一个空白或空格(值0)来代替目标值。显示错误ArrayIndexOutOfBoundsException是因为您正在使用nums.lenth-1int[]after=new int[nums.length-1]初始化after;
您可以更改该值,或者将目标元素与最后一个元素交换,并复制除最后一个元素之外的整个数组

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)) {
            //changing the target value with the last value of nums array
            nums[i]=nums[nums.length-1];
            nums[nums.length-1]=value;
        }
    }

    //Copying the entire array except the last
    for(int i=0;i<nums.length-1;i++){
        after[i]=nums[i];
    }
    return after;
}
publicstaticint[]remove(int[]nums,int-value){
int[]after=新int[nums.length-1];
对于(int i=0;i
public static int[] remove(int[] nums, int value){
    return IntStream.stream(nums).filter(i -> i != value).toArray();
}
.这是一艘1-liner:

public static int[] remove(int[] nums, int value){
    return IntStream.stream(nums).filter(i -> i != value).toArray();
}

你的问题是什么?上面的代码不起作用吗?如果是,它怎么会失败?如果
value
不是
nums
数组中的最后一个元素,你会得到
ArrayIndexOutOfBoundsException
,因为
after
length比
num
length小1,而
i
nums.length-1
一样高KyrSt说,你需要添加错误消息,以便这个问题对其他程序员有帮助。当你运行它时会发生什么?你的问题是什么?上面的代码不工作吗?如果是,它怎么会失败?如果
value
不是
nums
数组中的最后一个元素,你会得到
ArrayIndexOutOfBoundsException
,因为
>在
length比
num
length小1,而
i
则高达
nums.length-1
。正如KyrSt所说,您需要添加错误消息,以便这个问题对其他程序员有所帮助。运行它时会发生什么?