【问题标题】:How to check if a string equals any elements in a previously defined string array如何检查字符串是否等于先前定义的字符串数组中的任何元素
【发布时间】:2017-10-21 08:56:58
【问题描述】:

我创建了一个ArrayList,其中包含我从互联网上检索到的常用密码,并初始化了一个名为commonPasswords 的数组。我想检查用户输入的密码是否与数组中的任何密码匹配。但是,这似乎不起作用。我不知道为什么。任何帮助将不胜感激。

我刚开始学习如何编程,所以我是该领域的新手。谢谢!

int commonpass = 0;
int check = 0;
while (commonpass == 0) {
    if (password.equals(commonPasswords.get(check))) {
        score = 0;
    }
    check++;
    if (check >= commonPasswords.size()) {
        commonpass++;
    }
}

【问题讨论】:

标签: java arrays string arraylist


【解决方案1】:

OP 说他想check to see if the user inputted password matches 所以我认为他们的意思是相同的。

如果 OP 确实在寻找相同的条目,则 OP 应该使用 String.equals 而不是 Array.contains。因为Array.contains 可能会给他一个假阳性结果。

private void checkPasswordExists(){
    List<String> passwordList = Arrays.asList("pass1", "pass2", "pass12", "pass123");

    int countContains = 0;
    String userPassword = "pass1";
    for(String password : passwordList){
        if(password.contains(userPassword)){
            countContains++;
        }
    }

    int countEquals = 0;
    for(String password : passwordList){
        if(password.equals(userPassword)){
            countEquals++;
        }
    }

    System.out.println("Password countContains = " + countContains);
    System.out.println("Password countEquals = " + countEquals);
}

以上代码写入控制台:

Password countContains = 3
Password countEquals = 1

【讨论】:

    【解决方案2】:

    因为您是初学者,所以我为您编写了以下代码,演示了 4 种可能的非常简单的方法来做您想做的事情。 不建议初学者使用 Java 8 Stream API 和 Lambda 表达式。

        List<String> commonPasswords = Arrays.asList("touraj", "ttt", "toraj", "123");
        String userPassword = "123";
    
        //First Way:
        if (commonPasswords. contains(userPassword)) {
            System.out.println("Password Found");
        } else
        {
            System.out.println("Password Not Found");
        }
    
        //Second Way: foreach :: not suggested for beginners
        for (String commonPassword : commonPasswords) {
    
            if (commonPassword.equals(userPassword)) {
                System.out.println("Password Found");
    
                // here i use break after finding password to exit loop in order to not wasting cpu
                break;
    
            }
    
        }
    
        //Third Way: simple for loop :: suggested for beginners
        for (int i = 0; i <commonPasswords.size() ; i++) {
    
            if (commonPasswords.get(i).equals(userPassword)) {
                System.out.println("Password Found");
    
            }
        }
    
        //Forth way: Using Java 8 Stream Api :: Not Suggested for beginners like you
        boolean isPassFound =  commonPasswords.stream().anyMatch(pass -> pass.equals(userPassword));
        if (isPassFound) {
            System.out.println("Password Found.");
    
        }
    

    注意:为了理解 java 8 代码,我在这里建议你首先需要学习面向对象和接口,然后学习匿名方法,然后学习 lambda 表达式,然后学习 Stream API……但我认为希望 java 8 版本是自学的解释性的,有点类似于人类语言。

    【讨论】:

      【解决方案3】:

      正如钱德勒所说,你应该使用commonPasswords.contains(str) 而不是password.equals(commonPasswords.get(check))

      if commonPasswords.contains(password)
          return true;
      

      或者

      return commonPasswords.contains(passwords);
      

      【讨论】:

      • contains 已经有一个循环。无需再次添加。只是增加了复杂性
      • 哦,是的,我不知道我在想什么。固定。
      • 你可以这样做return commonPasswords.contains(password)
      • 是的,你可以,但他是编程新手,我认为 if 语句更直观。
      • 不,它不直观。这是不好的做法。对于初学者来说,将字符串与== 进行比较似乎很直观。但这是错误的。
      【解决方案4】:

      使用 Java 8,您可以按照以下方式进行操作

      List<String> commonPasswords = Arrays.asList("A", "B", "C");
      return commonPasswords.stream().anyMatch(str -> str.equals(password));
      

      【讨论】:

      • 你在这里把事情复杂化了。不要仅仅因为必须使用 lambda,而是在有意义的时候使用它。没有什么比这里简单的contains 更好了
      • 你也可以.anyMatch(password::equals);
      【解决方案5】:

      改用List#contains,比如

      if(commonPasswords.contains(password)){
          System.out.println("Password is not safe");
      }
      

      【讨论】:

        猜你喜欢
        • 2011-12-23
        • 2013-09-27
        • 2019-03-13
        • 2013-10-03
        • 2022-01-04
        • 2013-10-31
        • 2014-09-24
        • 1970-01-01
        • 2015-09-08
        相关资源
        最近更新 更多