简体   繁体   English

确保所有输入都是唯一数字的最佳方法是什么

[英]What is the best way to ensure that all inputs are all unique Numbers

Here is an example inputs:这是一个示例输入:

String test = "1 2 3 4 5 42";字符串测试 = "1 2 3 4 5 42"; String test2 = test.replaceAll(" ","");字符串 test2 = test.replaceAll(" ","");

public static boolean uniqueNumbers(String test2) {
    char[] testEntries= test2.toCharArray();
    Set<Character> set = new HashSet<>();
    
    for (int i = 0 ; i < testEntries.length ; i++ ) {
        if(!set.add(testEntries[i])) {
            return false;
        }
    }
    return true;
}

Despite all are unique number, it will return as false.尽管都是唯一的数字,它会返回为假。 Is there a way to fix this?有没有办法来解决这个问题?

You should split your input argument with split() at first.您应该首先使用 split() 拆分输入参数。

String test = "1 2 3 4 5 42";
String[] origin = test.split(" ");
Set<String> check = new HashSet<>(Arrays.asList(origin));
for (String el : check) {
    System.out.println(el);
}

Catch IllegalArgumentException from Set.ofSet.of捕获IllegalArgumentException

The Set.of convenience method in Java 9+ creates a Set of an unspecified implementation when passed an array. Java 9+ 中的Set.of便捷方法在传递数组时创建了一个未指定实现的Set If any duplicates are encountered while building this Set , an exception is thrown.如果在构建此Set时遇到任何重复项,则会引发异常。 You can trap for that IllegalArgumentException to know if the parts of your string are distinct or not.您可以捕获该IllegalArgumentException以了解您的字符串的各个部分是否不同。

We can get an array of the parts of your string by calling String#split .我们可以通过调用String#split来获取字符串部分的数组。

Boolean isDistinct;
try
{
    Set.of( "1 3 3 4 5 42".split( " " ) );
    isDistinct = Boolean.TRUE;
}
catch ( IllegalArgumentException e )
{
    isDistinct = Boolean.FALSE;
}

System.out.println( "Input is distinct: " + Objects.requireNonNull( isDistinct ) );

See this code run live at Ideone.com .请参阅在 Ideone.com 上实时运行的代码

Input is distinct: false输入不同:假

By the time you are passing your String parameter to uniqueNumbers() it looks like当您将 String 参数传递给 uniqueNumbers() 时,它看起来像

1234542

Splitting it into char array takes each single character as a separate element, printing out the output immediately for instance gives:将其拆分为 char 数组将每个单个字符作为一个单独的元素,立即打印输出,例如:

char[] testEntries = test2.toCharArray();
System.out.println(Arrays.toString(testEntries));
//output: [1, 2, 3, 4, 5, 4, 2]

So when passing each element to the set, it finds 2 repeated at index 1 and 6.因此,当将每个元素传递给集合时,它会发现 2 在索引 1 和 6 处重复。

One solution in this case would be to pass the original to split test using String's instance split().在这种情况下,一种解决方案是使用 String 的实例 split() 通过原始拆分测试。

    String test = "1 2 3 4 5 42";
    String[] test2=test.split(" ");


    public static boolean uniqueNumbers(String testEntries[]) {

    Set<String> set = new HashSet<>();

    for (int i = 0; i < testEntries.length; i++) {
        if (!set.add(testEntries[i])) {
            return false;
        }
    }
    return true;
}

//output: true

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

相关问题 代表给定范围内所有数字的最佳方法是什么? (一些限制) - What is the best way to represent all numbers in a given range? (some restrictions) 在哈希图中获取所有元素的最佳方法是什么? - What is the best way to get all elements in a hashmap? 原子替换Java中List中所有内容的最佳方法是什么? - What is the best way to atomic-replace all contents in List in Java? 在所有活动中存储和访问变量的最佳方法是什么? - What is the best way to store and access variables in all activities 在Java类中打印字段的所有值的最佳方法是什么 - what is the best way to print all the values of the fields in a java class 解决所有Java字节都已签名这一事实的最佳方法是什么? - What is the best way to work around the fact that ALL Java bytes are signed? 为所有响应定义公共基础 Java Model 的最佳方法是什么? - What is the best way to define a common Base Java Model for all Responses? 从抽象类的所有子类调用方法的最佳方法是什么? - What is the best way to call methods from all the subclasses of an abstract class? 等待线程池中所有工作程序完成的最佳方法是什么? - What is the best way to wait for the completion of all workers in a thread pool? 验证所有响应数据的最佳方法是什么? - What is the best way to verify the all response data in restassured?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM