对于某些数组整数,我的 remove 方法有什么问题?

我必须创建一个方法,该方法将从数组中删除特定值,并创建一个没有该特定值的新数组。例如,如果我的数组是 (0,2,3,5,3),而我想删除 3,则新数组应该是 (0,2,5)。由于某种原因,它仅适用于前两位数字。


import java.util.Scanner;


public class removeDemo {


    public static void main(String[] args) {

        // TODO Auto-generated method stub

        //array of numbers

        int array[] = new int[] {0,1,2,3,4,5};

        //invokes method and prints result

        //System.out.println(remove(3,array));

        remove(3,array);



    }

    //method remove that removes selected number from array

    public static int[] remove(int v, int[] in) {

        //count variable counts how many non-target numbers

        int count = 0;

        //for loop that checks if value at certain index is not equal to "v", the target number for removal

        for(int k = 0; k < in.length; k++) {

            //checks if certain number at certain index of array is not equal to v, or in this case, 3


            if(in[k] != v) {

                //counter

                count++;


            }

    }


        //new array that will stores values except "v"

        int copy[] = new int[count];

        //prints the length

        System.out.println("array length: " + copy.length);

        //for loop that checks if number not 3

        for(int a = 1; a < in.length;) {

        //  sets number at certain index of main array into new array

            if(in[a] != 3){

                copy[a] = in[a];

                a++;


                System.out.println(copy[0]);

                System.out.println(copy[1]);

                System.out.println(copy[2]);

                System.out.println(copy[3]);



            }

            else if(in[a] == 3) {


                copy[a] = in[a+1];

            }



        }


        //returns new array

        return copy;

}


}

如前所述,我需要新数组来排除要删除的目标编号。


慕少森
浏览 106回答 3
3回答

慕莱坞森

您可以使用如下所示的一些代码获得相同的结果:// Add to params all inputs to remove from arrayList<Integer> params = new ArrayList<>();// Use Integer class instead of int datatypeInteger array[] = new Integer[] {0,1,2,3,4,3};// Convert array to List classList<Integer> list = new ArrayList<>(Arrays.asList(array));// Remove all matcheslist.removeAll(params);

慕丝7291255

您需要两个索引变量来进行复制:一个贯穿输入数组(如原始代码中所示),另一个跟踪您在输出数组中的位置(new 变量)。它们不能相互计算(它们在开始时是相同的,但可以明显小于最后)abbaint b = 0;for(int a = 0; a < in.length; a++) {&nbsp; if(in[a] != v) {&nbsp; &nbsp; copy[b] = in[a];&nbsp; &nbsp; b++;&nbsp; }}

湖上湖

使用Java8及其流功能,您可以执行如下操作:&nbsp;public static void main(String[] args) {&nbsp;int[] array = {3236,47,34,34,73,46,3,64,473,4,4,346,4,63,644,4,6,4};&nbsp;int[] newArray = removeAllOccurencesOf(array, 4);&nbsp;System.out.println(Arrays.toString(newArray));&nbsp;}&nbsp;public static int[] removeAllOccurencesOf(int[] array, int numberToRemove)&nbsp;{&nbsp;//stream integers from array, filter the ones that correspond to number to remove, get what's left to new array&nbsp;int[] newArray = IntStream.of(array).filter(i->i!=numberToRemove).toArray();&nbsp;return newArray;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java