简体   繁体   English

向后检查阵列中的重复项

[英]Checking for Duplicates in an Array Backwards

This is the same person who had trouble with the last array problem just one or two days ago. 这是同一个人,在一两天前就遇到了最后一个阵列问题。 We've a new assignment which asks us to find and replace duplicates in an array of randomly generated numbers. 我们有一个新任务,要求我们在随机生成的数字数组中查找和替换重复项。 I wrote a code and sent it to my teacher for feedback; 我编写了一个代码,并将其发送给我的老师以征求反馈。 she responded with this solution: 她以以下解决方案回应:

So, take the first random num and store into the first slot (this can be done before the loop). 因此,取第一个随机数并存储到第一个插槽中(可​​以在循环之前完成)。 Then, start a loop that creates the second random num and tests backwards to see if there are duplicates from the ones already stored. 然后,启动一个循环,该循环创建第二个随机数并向后测试以查看是否已存储的重复项。 So, a backwards loops that tests for duplicates and counts down to 0 from the current location and replaces duplicates. 因此,向后循环会测试重复项,并从当前位置开始递减至0并替换重复项。 Once that test passes, then you'll go to the next element, create a new random number, and then test the ones before it for duplicates. 测试通过后,您将转到下一个元素,创建一个新的随机数,然后对之前的数字进行重复测试。

I've done this here, and it's reduced the number of randomly generated numbers, but I still run into the stray duplicate: 我在这里做了这个,减少了随机生成的数字的数量,但是我仍然遇到了流浪的副本:

import java.lang.Object;
    import java.util.Random;

public class Prog433a {

    public static void main(String[]args) {

        Random randslct = new Random();

        int[] list = new int[20];
        int counter = 0;
        int index = 0;
        int min2 = 0;


        System.out.println("\nAfter");

        for (int k = 0; k < list.length - 1; k++) {
            list [k] = randslct.nextInt(30) + 1;
            for (int z = list.length - 1; z >= 0; z--) {
                if (list[k] == list[z] && z!=k) {
                    while (list[k] == list[z]) {
                        list [k] = randslct.nextInt(30) + 1; 
                    }
                }
            }
        }

        int min = list[0];
        while (counter < list.length - 1) {
            for (int x = 0; x < list.length - 1; x++) { // scroll through the indexes.
                if (list[x] < min) {
                    min = list[x];
                    index = x; // keep the index of the biggest number.
                } 
            }
            System.out.println(list[index]);
            min = 100 * (list[index]);

            list[index] = 100 * (list[index]); // change the value in the original array so it won't find the same max again
            counter++;
        }
    }
}

System Output: 系统输出:

After
2
5
6
10
11
12
13
15
16
17
19
22
22
24
25
27
28
29
29


After
1
2
2
4
5
7
8
9
10
13
15
16
21
24
25
26
28
29
30

After
1
2
3
5
6
7
11
12
13
14
15
16
18
21
22
25
26
27
29

After
2
3
3
4
6
10
12
14
15
16
17
20
22
23
24
25
26
27
30

After
7
8
11
12
13
14
15
16
17
17
18
19
20
21
23
24
27
29
30

I posted my output towards the bottom. 我将输出发布到底部。 Because this is an introductory coding class, I'd prefer if the solution did not involve Sets or any of the like. 因为这是入门级的编码类,所以如果解决方案不涉及Sets或类似对象,则我更愿意。 But alas, beggars cannot be choosers. 但是a,乞g不能成为选择者。

Is there something I have forgotten to add? 有什么我忘了补充的吗?

Your problem is that when you detect a duplicate you generate a new number, but you never go back and check that the the newly generated number is not a duplicate of the numbers you already checked. 您的问题是,当您检测到重复项时,您会生成一个新号码,但是您再也不会回过头来检查新生成的号码是否与您已经检查过的号码重复。 When you run into a duplicate you'll need to reset the checking loop through some mechanism. 当您遇到重复项时,您需要通过某种机制来重置检查循环。

I fixed up the code to work around the problem, but it's not the prettiest solution. 我修复了代码以解决此问题,但这不是最漂亮的解决方案。 I also did some minor optimisation as you were looping through unnecessary indices. 当您遍历不必要的索引时,我还做了一些小的优化。

import java.util.Random;

public class Prog433a {

    public static void main(String[] args) {

        Random randslct = new Random();

        int[] list = new int[20];
        int counter = 0;
        int index = 0;
        int min2 = 0;

        System.out.println("\nAfter");

        for(int k = 0; k < list.length - 1; k++) {

            list[k] = randslct.nextInt(30) + 1;
            boolean unique = true;
            for(int z = k - 1; z >= 0; z--) {
                if(list[k] == list[z]) {
                    if(list[k] == list[z]) {
                        unique = false;
                        break;
                    }
                }
            }
            if(!unique) {
                // Repeat last index
                --k;
            }
        }

        int min = list[0];
        while(counter < list.length - 1) {
            for(int x = 0; x < list.length - 1; x++) { // scroll through the indexes.
                if(list[x] < min) {
                    min = list[x];
                    index = x; // keep the index of the biggest number.
                }
            }
            System.out.println(list[index]);
            min = 100 * (list[index]);

            list[index] = 100 * (list[index]); // change the value in the original array so it won't find the same max again
            counter++;
        }

    }

}

Your mistake is when you try to add a new number. 您的错误是当您尝试添加新号码时。 You just check, if it isn't the same as the one before, but not if it is the same as twice before. 您只需要检查它是否与以前的不相同,就可以检查是否与以前的两次相同。 You can do this as follow: 您可以按照以下步骤进行操作:

boolean isDuplicate(int index, int[] list){
    for(int i=index-1; i>=0;i--){
        if(int[i]==int[index])
            return false
    }
    return true;
}

instead of your inner of the for-loop you can now write: 现在,您可以编写以下代码来代替for循环的内部内容:

do{
    list[k] = randslct.nextInt(30) + 1;
}while(isDuplicate(k, list));

Also you should change your output, eg give the already written output a negative value and ignore negative values. 另外,您应该更改输出,例如,给已写入的输出一个负值,而忽略负值。 If you want to change the numbers up to eg 200 your code now won't work. 如果您想将数字最多更改为200,则您的代码现在将无法工作。

Lets take this by example. 让我们以这个为例。 Consider that the current list that has been generated is: 考虑已生成的当前列表为:

list = [5, 7, 9, 3, 8, 9]

where 9 is the current number. 其中9是当前数字。

Now in the for-loop, you iterate from list[6] to list[0]. 现在在for循环中,您从list [6]迭代到list [0]。 Here, in comparision, you come to 2nd index (list[2]) where the condition 在比较中,您来到第二个索引(列表[2]),其中条件

list[k] == list[z] && z != k

turns out to be true and a new random number is generated. 事实为真,并生成一个新的随机数。 Lets assume that here the new random number that you generated is '8'. 让我们假设您在此生成的新随机数为“ 8”。 The loop terminates successfully and your array now has a duplicate. 循环成功终止,并且您的阵列现在具有重复项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM