Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java数组查找重复项并替换它们 公共静态int[]是重复的(int[]组合){ Set foundedNumbers=new HashSet(); for(int i=0;索引_Java_Arrays - Fatal编程技术网

java数组查找重复项并替换它们 公共静态int[]是重复的(int[]组合){ Set foundedNumbers=new HashSet(); for(int i=0;索引

java数组查找重复项并替换它们 公共静态int[]是重复的(int[]组合){ Set foundedNumbers=new HashSet(); for(int i=0;索引,java,arrays,Java,Arrays,我需要找到并替换数字数组中的重复项。数字数组也是由1到40之间的7个数字随机选择而成。如果我有一个重复项,我给出的这段代码可以工作,但当我有多个重复项时,例如,我有1,14,20,1,38,1,5。中间的1会改变,但第二个1会保持不变。我相信这就解决了你的问题: 首先,确定数组中的数字,并将其记录为尚未找到 然后你把这些数字看一遍,当你找到一个还没有找到的数字时,你就把它标记为找到了 当你再次找到这个数字时,你可以用它做任何你想做的事情(在这个例子中,我把它设为-1) 公共静态int[]重复项(

我需要找到并替换数字数组中的重复项。数字数组也是由1到40之间的7个数字随机选择而成。如果我有一个重复项,我给出的这段代码可以工作,但当我有多个重复项时,例如,我有1,14,20,1,38,1,5。中间的1会改变,但第二个1会保持不变。

我相信这就解决了你的问题:

首先,确定数组中的数字,并将其记录为尚未找到

然后你把这些数字看一遍,当你找到一个还没有找到的数字时,你就把它标记为找到了

当你再次找到这个数字时,你可以用它做任何你想做的事情(在这个例子中,我把它设为-1)

公共静态int[]重复项(int[]组合){
HashMap foundNumbers=新HashMap();
for(int i:组合){
如果(foundnumber.containsKey(i))继续;
输入(i,false);
}
for(int i=0;i
您的代码似乎只需稍加修改和位转换即可工作

public static int[] duplicates(int[] combination) {
            HashMap<Integer, Boolean> foundNumbers = new HashMap<>();
            for (int i : combination) {
                if(foundNumbers.containsKey(i)) continue;
                foundNumbers.put(i, false);
            }

            for (int i = 0; i < combination.length; i++) {
                if(!foundNumbers.get(combination[i])) {
                    foundNumbers.put(combination[i], true);
                } else {
                    combination[i] = -1;
                }
            }

            return combination;
        }
您将获得所有重复编号的替换

各种处决:

int[] i = {1,14,20,1,38,1,5};
System.out.println(Arrays.toString(imaliDuplikata(i)));

为什么你定义了一个HashSet却从不使用它?(
nadjeniBrojevi
)当你将变量名翻译成英语(为什么不是英语?)时,请翻译所有变量名。你的参数和集合仍然需要翻译。你能告诉我们你发布的输入(1,14,20,1,38,1,5)吗,你想要的结果是什么?我不确定你是否能做到这一点,但在1-40之间生成7个唯一的数字不是很容易吗?对不起,汤姆,现在我更改并翻译了所有内容。关于输入,数组的元素是什么并不重要,在我的代码中,我必须为数组生成随机数,我只是想当我测试代码时,我发现当我有多个相同的元素时,它会变得混乱。它可能会返回重复的值,例如:
[1,14,20,38,38,39,5]
r.nextInt(40)
可以一直返回相同的数字Jordi,我相信它是有效的,因为我尝试了10次,但我从来没有重复过。但我会尝试找出其他解决方案,以确保它总是正确的。Mapet@Uata说的是正确的,你可以用这个解决方案重复,因为你得到一个随机数,但你可以添加一个要检查随机整数是否已经在映射中,并且不添加它,否则,获取一个新的随机值…Arno我也尝试了你的代码,因为我理解你的步骤,但它一直在重复。无论如何,谢谢。
public static int[] imaliDuplikata(int[] combination) {
    Random r = new Random();
    Set<Integer> foundeNumbers = new HashSet<>();

    for (int i = 0; i < combination.length; i++) {
        if (foundeNumbers.contains(combination[i])) {
            combination[i] = r.nextInt(40);
        } else {
            foundeNumbers.add(combination[i]);
        }

    }
    return combination;
}
int[] i = {1,14,20,1,38,1,5};
System.out.println(Arrays.toString(imaliDuplikata(i)));
[1, 14, 20, 0, 38, 35, 5]
[1, 14, 20, 11, 38, 1, 5]
[1, 14, 20, 22, 38, 30, 5]
[1, 14, 20, 37, 38, 39, 5]