试图消除数组中的重复但不工作

时间:2014-03-11 17:43:09

标签: java

这是技术上的一项新任务,但我这样做是为了练习而不是为了成绩。到目前为止,我的解决方案存在问题:

/**
     * 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--;
        }
    }

}

编辑:这里更新的代码是我的结果:

让smoosh阵列!

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  ]

我越来越近但它仍然工作不正常,所以我的逻辑肯定有问题

2 个答案:

答案 0 :(得分:0)

也许你应该看看如何使用收藏夹。看看Java Tutorials

我会按如下方式处理你的问题:

  1. 创建Set对象
  2. 将数组中的值插入集合中(一个集合不能重复,因此不允许插入一个),并且
  3. 返回结果集(或将其转换为数组),并根据需要执行任何操作。
  4. 所以,它会是这样的:

    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个值),因为Collection只能保存对象。您可以将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
    // ...
    

    一些有用的链接:

    希望这有帮助

答案 1 :(得分:0)

例如,您可以按照以下方式执行此操作:

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]