java—试图消除数组中的重复项,但不起作用

jvidinwx  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(196)

这是一个硬件作业技术上,但我做它的实践,而不是一个等级。到目前为止,我的解决方案存在以下问题:

/**
     * smoosh() takes an array of ints. On completion the array contains the
     * same numbers, but wherever the array had two or more consecutive
     * duplicate numbers, they are replaced by one copy of the number. Hence,
     * after smoosh() is done, no two consecutive numbers in the array are the
     * same.
     * 
     * Any unused elements at the end of the array are set to -1.
     * 
     * For example, if the input array is [ 0 0 0 0 1 1 0 0 0 3 3 3 1 1 0 ], it
     * reads [ 0 1 0 3 1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 ] after smoosh()
     * completes.
     * 
     * @param ints
     *            the input array.
   **/

    public static void smoosh(int[] ints) {
    // Fill in your solution here. (Ours is fourteen lines long, not
    // counting
    // blank lines or lines already present in this file.)

    int index = ints.length - 1;
    for (int i = 1; i < ints.length; i++) {
        if (ints[i] == ints[i - 1]) {
            for (int j = i; j < ints.length - 1; j++) {
                ints[j] = ints[j + 1];
            }
            ints[index] = -1;
            index--;
        }
    }

}

编辑:更新代码这里是我的结果:
让我们尽情享受吧!

smooshing [  3  7  7  7  4  5  5  2  0  8  8  8  8  5  ]:
[  3  7  7  4  5  2  0  -1  -1  -1  -1  -1  -1  -1  ]

***ERROR:  BAD SMOOSH!!!  No cookie.

java.lang.Exception: Stack trace
    at java.lang.Thread.dumpStack(Unknown Source)
    at hw3.TestHelper.verify(TestHelper.java:26)
    at hw3.Homework3.main(Homework3.java:72)
smooshing [  6  6  6  6  6  3  6  3  6  3  3  3  3  3  3  ]:
[  6  6  6  3  6  3  -1  -1  -1  -1  -1  -1  -1  -1  -1  ]

***ERROR:  BAD SMOOSH!!!  No cookie.

java.lang.Exception: Stack trace
    at java.lang.Thread.dumpStack(Unknown Source)
    at hw3.TestHelper.verify(TestHelper.java:26)
    at hw3.Homework3.main(Homework3.java:82)
smooshing [  4  4  4  4  4  ]:
[  4  4  -1  -1  -1  ]

***ERROR:  BAD SMOOSH!!!  No cookie.

java.lang.Exception: Stack trace
    at java.lang.Thread.dumpStack(Unknown Source)
    at hw3.TestHelper.verify(TestHelper.java:26)
    at hw3.Homework3.main(Homework3.java:91)
smooshing [  0  1  2  3  4  5  6  ]:
[  0  1  2  3  4  5  6  ]

我有点接近,但它仍然工作不正常,所以一定是我的逻辑有问题

yqhsw0fo

yqhsw0fo1#

也许你应该看看如何使用集合。请看java教程。
我将按以下方式解决你的问题:
创建 Set 对象
在集合中插入数组中的值(集合不能有重复项,因此不允许您插入重复项),然后
返回结果集(或将其转换为数组)并对其执行任何需要执行的操作。
所以,应该是这样的:

public Integer deduplicateArray(int[] values) {
    Set<Integer> intSet = new HashSet<>(values.length);
    for(Integer i : values) {
        if(!intSet.add(i))
            System.out.println("Value " + i + " is duplicated and was not added");
    }
    return intSet.toArray();
}

请注意,您指定集合将包含 Integer 对象(非 int 值),因为集合只能容纳对象。你可以使用 Integer 对象作为任何其他对象 int 价值:

// ...
int aValue; // This is a int (primitive) variable
Integer someInteger = 12; // This is an Integer object
aValue = someInteger + 3; // aValue is still a `int` (primitive) variable
// ...

一些有用的链接:
set接口(java教程)
这个 HashSet 应用程序编程接口
希望这有帮助

li9yvcax

li9yvcax2#

例如,您可以按以下方式执行:

int[] array = {0, 1, 0, 3, 1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1};

Set<Integer> set = new TreeSet<>();

for (int i : array) {
    set.add(i);
}

System.out.println("set = " + set);

结果是:

set = [-1, 0, 1, 3]

相关问题