java - 向后检查数组中的重复项

标签 java

这就是一两天前遇到最后一个数组问题的同一个人。 我们有一个新作业,要求我们在随机生成的数字数组中查找并替换重复项。我写了一个代码并发送给我的老师寻求反馈;她用这个解决方案回应:

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. 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.

我在这里这样做了,它减少了随机生成的数字的数量,但我仍然遇到了杂散的重复:

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++;
        }
    }
}

系统输出:

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

我将我的输出发布到底部。 因为这是一个介绍性编码类(class),所以我希望解决方案不涉及集合或任何类似内容。但可惜的是,乞丐不能挑剔。

有什么我忘记添加的吗?

最佳答案

您的问题是,当您检测到重复项时,您会生成一个新号码,但您永远不会返回并检查新生成的号码是否与您已检查的号码重复。当您遇到重复项时,您需要通过某种机制重置检查循环。

我修复了代码来解决该问题,但这不是最漂亮的解决方案。当您循环访问不必要的索引时,我还做了一些小的优化。

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++;
        }

    }

}

关于java - 向后检查数组中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35577432/

相关文章:

java - 丢失数组中的父类(super class)属性 - Java

java - 在Java中将3个整数转换为日期变量

java - 如何在 servlet 中调用带参数的 URL 并获取响应?

Java Swing 布局管理器循环引用被设置两次

java - 如何使抽屉菜单链接到 Android Studio 中的其他 fragment

java - struts 2.3上传前预览图片

java - 如何在c#中复制java加密

java - 如何检查为 8085 模拟器设置的标志

java - Spring Boot 上的 QueryDSL 问题

java - 具有泛型方法的泛型类的协变