java 比较两个字符串数组以检查输入的值是否存在于字符串数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5430017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:04:55  来源:igfitidea点击:

comparing two string arrays to check if the entered value is there in the string array

java

提问by sahana

I have two string arrays

我有两个字符串数组

String[] Mids contains 
MSI
MSU
MSV

String[] sl contains
    MSU
    MSV
    MSS

Actual output should be

实际输出应该是

Action 
Action 
Action cannot be set

for(int i=0;i<sl.length;i++){
                for(int j=0;j<Mids.length;j++){
                    if((sl[i].equals(Mids[j]))){
                       System.out.println("Action");
                    }else{
                        System.out.println("Action cannot be set");
                    }
                }
            }

OUTPUT which i get

我得到的输出

Action cannot be set
Action cannot be set
Action cannot be set
Action cannot be set
Action
Action cannot be set
Action
Action cannot be set
Action cannot be set

回答by RoflcoptrException

The problem is you're iterating over both arrays and always printing if you have found the same value. But you should do this only in the first loop. I changed the for loop:

问题是您要遍历两个数组,并且如果找到相同的值,则总是打印。但是您应该只在第一个循环中执行此操作。我改变了 for 循环:

for(int i=0;i<sl.length;i++){
   boolean found = false;
   for(int j=0;j<Mids.length;j++){
      if((sl[i].equals(Mids[j]))){
         found = true;
         break;
      }
   }

   if (found) {
      stdOut.println("Action");
   } else {
      stdOut.println("Action cannot be set");
   }
}

回答by codaddict

To say if an element is not found in an array you need to compare it with all the elements. Just because one comparison fails you cannot conclude that it is not found in the array.

要说是否在数组中找不到元素,您需要将它与所有元素进行比较。不能仅仅因为一个比较失败就断定它没有在数组中找到。

Try something like:

尝试类似:

for(int i=0;i<sl.length;i++){
        boolean found = false;
        for(int j=0;j<Mids.length;j++){
                if((sl[i].equals(Mids[j]))){
                        found = true;
                        break;
                }
        }
        if(found) {
                // print found.
        } else {
                // print not found.                                             
        }
}

回答by Andrew A.

Another way to do it, with fewer lines of code and fewer iterations would be:

使用更少的代码行和更少的迭代来做到这一点的另一种方法是:

List<String> midsList = new ArrayList<String>(Arrays.asList(Mids));
    for (String string : sl) {
        if (midsList.contains(string)) {
            System.out.println("Action");
        } else {
            System.out.println("Action cannot be set");
        }
    }

回答by tamarintech

Why don't you add another printline under the for loop(i) that displays s1 and mids so that you can better understand the execution?

为什么不在显示 s1 和 mids 的 for loop(i) 下添加另一个打印行,以便更好地理解执行?