猿问

Java删除数组中的冗余项

对于这个特定问题,我试图删除排序数组中的冗余元素,并在数组末尾将它们全部替换为 0 。例如,如果我有一个由 int 元素组成的数组


1,3,3,4,4,5,6,6,7


我的输出数组应该是


1,3,4,5,6,7,0,0,0


我对这个问题的第一次尝试是创建一个交换器,以便在删除元素后将所有 0 推送到列表的末尾,但它似乎不会将零推送到列表的末尾。这是我的代码。


 public void implode(int[] ary) 

    {

        int swapper = -1;


        int[] newARY = new int[ary.length];

        int current = -1;


        for (int i = 0; i < ary.length; i++)

        {

            if (current != ary[i])

            {

            newARY[i] = ary[i];

            current = ary[i];

            }


        }


        for (int i = 0; i < ary.length; i++)

        {

            if (ary[i] == 0)

            {

                if (ary[i + 1] != 0)

                {

                    swapper = ary[i + 1];

                    ary[i] = swapper;

                    ary[i + 1] = 0;

                }


            }


        }


        ary = newARY;

        for (int i = 0; i < newARY.length; i++)

        {

            System.out.print(newARY[i] + " ");

        }


    }

我用它测试的数组是,


 int[] aryIn2 = {1, 1, 2, 3, 4, 4, 5, 6};

但是,当输出内爆阵列时,我收到了这个。


1 0 2 3 4 0 5 6


有什么我想念的吗?


提前致谢。


当年话下
浏览 169回答 3
3回答

狐的传说

我观察到的代码有两个问题。1) 您的交换器逻辑在与您之前进行修改的阵列不同的阵列上执行交换2)您需要以冒泡排序的方式拥有此逻辑,即在循环内循环以下是您的方法的有效修改示例代码。我只修改了第二个 for 循环逻辑public void implode(int[] ary) {&nbsp; &nbsp; int swapper = -1;&nbsp; &nbsp; int[] newARY = new int[ary.length];&nbsp; &nbsp; int current = -1;&nbsp; &nbsp; for (int i = 0; i < ary.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (current != ary[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newARY[i] = ary[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current = ary[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for (int i = 0; i < newARY.length - 1; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (newARY[i] == 0 && newARY[i + 1] != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = i; (j + 1) < newARY.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swapper = newARY[j + 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newARY[j] = swapper;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newARY[j + 1] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for (int i = 0; i < newARY.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(newARY[i] + " ");&nbsp; &nbsp; }}

繁星淼淼

不是您问题的答案,但使用(如果可能)Java 流可以缩短您的方式:int[] arr = {1,3,3,4,4,5,6,6,7};// distinctList<Integer> list = Arrays.stream(arr).distinct().boxed().collect(Collectors.toList());// pad with zero'swhile(list.size() < arr.length) {&nbsp; &nbsp; list.add(0);}// displaySystem.out.println(list.stream().map(String::valueOf).collect(Collectors.joining(",")));会输出1,3,4,5,6,7,0,0,0

饮歌长啸

在第一个循环中:for (int i = 0; i < ary.length; i++) {&nbsp; &nbsp; if (current != ary[i]) {&nbsp; &nbsp; &nbsp; &nbsp; newARY[i] = ary[i];&nbsp; &nbsp; &nbsp; &nbsp; current = ary[i];&nbsp; &nbsp; }}你用重复的值填充newARY元素ary变成0:newARY: 1 0 2 3 4 0 5 6但是,在第二个循环中:&nbsp;for (int i = 0; i < ary.length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (ary[i] == 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ary[i + 1] != 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swapper = ary[i + 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ary[i] = swapper;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ary[i + 1] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }您正在修改原始ary数组。所以 newARY 没有更新。但是,如果连续有两个以上的 0,您将 0 推到数组末尾的尝试也会失败。而且它也容易受到 ArrayOutOfBoundIndexException 的影响,因为您尝试ary[i+1]不受限制地阅读i将 0 推到数组末尾的一种简单而直接的方法是创建具有非 0 元素的新数组,然后再填充 0:int[] result = new int[ary.lenght];int resultIndex = 0;for (int i = 0; i < newARY.length; i++) {&nbsp; &nbsp; if (newARY[i] != 0) {&nbsp; &nbsp; &nbsp; &nbsp; result[resultIndex++] = newAry[i];&nbsp; &nbsp; }}for (int i = resultIndex; i < newARY.length; i++) {&nbsp; &nbsp; result[i] = 0;}// Print result array提示:使用上述策略,您可以简化您的代码。无需创建直接数组 newARY。只需循环遍历原始数组,将唯一元素推送到结果数组,然后用 0 填充剩余的任何插槽。
随时随地看视频慕课网APP

相关分类

Java
我要回答