简体   繁体   English

比较两个String数组,分别返回匹配的String和不匹配的String

[英]Compare two String arrays , return matched Strings and mismatched Strings separately

I am developing an android application. 我正在开发一个android应用程序。 I am trying to compare two string arrays and return the matches , mismatches. 我试图比较两个字符串数组,并返回匹配项,不匹配项。 I tried couple of solutions but it's not working at all. 我尝试了几种解决方案,但它根本没有用。 I need matches and mismatches separately. 我需要分别匹配和不匹配。

Here are the arrays with example data 这是带有示例数据的数组

String[] number_one = { "info@fn.ca" , "+122637867"  , "486" , "smbr" , "9946567" };

String[] number_two = { "+122637867" , "486" , "nrkZone"  , "smbr'};

Here is the example code i am working on. 这是我正在处理的示例代码。

          for(int n =0; n < number_one.length; n++){

            if(number_one[n] == number_two[n]){
                 Log.d("Cursor" ,number_one[n]);
            }else{
                 Log.d("Cursor" ,number_two[n]);
             }

           }

I want outputs like (Example data) 我想要类似(示例数据)的输出

 Matched : +122637867 , 486 , smbr
 Mismatched :  info@fn.ca , nrkZone , 9946567

I probably did more than I should have, and maybe even made it over complicated. 我所做的可能比我应该做的要多,甚至使事情变得复杂。 But I'll let you guys decide that. 但我会让你们决定。

The first thing that I did was to use HashSets to store the matched and mismatched values. 我要做的第一件事是使用HashSets存储匹配和不匹配的值。 This is useful because they only store one of each value. 这很有用,因为它们仅存储每个值之一。

String[] number_one = { "info@fn.ca" , "+122637867"  , "486" , "smbr" , "9946567" };
String[] number_two = { "+122637867" , "486" , "nrkZone"  , "smbr"};


HashSet<String> matched = new HashSet<String>();
HashSet<String> mismatched = new HashSet<String>();

Now I have quite a few for loops, this part I figure you could simplify into maybe just one. 现在我有很多for循环,我认为您可以将这一部分简化为一个。 But alas, it works. 但是a,它有效。 Now as to why . 现在说为什么 Well, the way this code works is by assuming all of the values in the arrays have no matches until proven otherwise. 好的,此代码的工作方式是假设数组中的所有值都没有匹配项,除非另外证明。 So it simply puts all of them into the mismatched HashSet. 因此,它只是将它们全部放入mismatched HashSet中。

for (int i = 0; i < number_one.length; i++) {
    mismatched.add(number_one[i]);
}

for (int i = 0; i < number_two.length; i++) {
    mismatched.add(number_two[i]);
}

Now you need to nest two for loops to iterate through both arrays, and in the process checking for any matches, and if there is one, it is added to the matched HashSet. 现在,您需要嵌套两个for循环以遍历两个数组,并在此过程中检查是否有任何匹配项,如果有匹配项,则将其添加到matched HashSet中。

for(int n = 0; n < number_one.length; n++) {
       for (int m = 0; m < number_two.length; m++) {
           if(number_one[n].equals(number_two[m])){
            matched.add(number_one[n]);
           }
       }
   }

Now we simply remove all of the values that we know have matches from the mismatched variable, which already has ALL of the possible values. 现在,我们简单地从不mismatched变量中删除我们知道具有匹配项的所有值,该变量已经具有所有可能的值。 We are simply removing what we know matches. 我们只是删除了我们所知道的匹配项。

for (int i = 0; i < matched.size(); i++) {
    mismatched.remove(matched.toArray()[i]);
}

And here we simply print them out in a semi-neat fashion. 在这里,我们仅以半整齐的方式将它们打印出来。

System.out.print("Matched: ");
for (int i = 0; i < matched.size(); i++) {
    System.out.print(matched.toArray()[i] + " ");
}

System.out.println("");
System.out.print("Mismatched: ");
for (int i = 0; i < mismatched.size(); i++) {
    System.out.print(mismatched.toArray()[i] + " ");
}

Try this code implemented using array. 尝试使用数组实现的这段代码。

    String[] number_one = { "info@fn.ca" , "+122637867"  , "486" , "smbr" , "9946567" };
    String[] number_two = { "+122637867" , "486" , "nrkZone"  , "smbr"};
    boolean mismatch=true;
    for(int i=0;i<number_one.length;i++){
        mismatch=true;
        for(int j=0;j<number_two.length;j++){
        if(number_one[i].equals(number_two[j])){
            System.out.println(number_one[i]+"Matches");
        mismatch=false;
        break;
        }
        }
    if(mismatch){
        System.out.println(number_one[i]+"Mis Matches");
    }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM