如何将一个字符串元素与字符串元素数组进行比较?

时间:2018-04-09 16:25:05

标签: java android arrays string function

我有一个比较2个字符串并打印出多少共同元素的函数。我目前的代码是:

public void StringCheck(String one, String two) { 

    String[] subStrings1 = one.split(", ");
    String[] subStrings2 = two.split(", ");

    Set<String> set1 = new HashSet<>();
    Set<String> set2 = new HashSet<>();

    for (String s : subStrings1) {
        set1.add(s);
    }
    for (String s : subStrings2) {
        set2.add(s);
    }
    set1.retainAll(set2);
    textView3.setText(set1.size() + "");
} 

当我调用这样的函数时:StringCheck("1, 2, 3, 4, 5" , "1, 2, 3, 4 ,5");它在我的android屏幕上打印出5

但我实际上想将我的第一个字符串与另一个字符串进行比较。例如,我想给一个字符串和一个字符串数组作为参数,看看有多少共同的元素。 假设我的第一个字符串是:"1, 2, 3, 4, 5"我想将此字符串与其他字符串进行比较。我们说, 第二个"2, 3, 4, 5, 6" 第三个"3, 4, 5, 6, 7"

我希望输出如下:

Result 1: 4 Result 2: 3

3 个答案:

答案 0 :(得分:0)

这有点粗糙,但这应该有效:

public void StringCheck(String one, String[] two) { 
    String result = "";

    String[] subStrings1 = one.split(", ");
    Set<String> set1 = new HashSet<>();

    // Add all the targets to a set
    for(String s: subStrings1)
    {
        set1.add(s);
    }

    // For each of the input strings in the array
    for(int i = 0; i < two.length; ++i)
    {
        // Keep track of the total, and split based on the comma
        int total = 0;
        String[] subStrings2 = two[i].split(", ");

        // For each of the substrings
        for(String s2: subStrings2)
        {
            // If the set contains that substring, increment
            if(set1.contains(s2))
            {
                ++total;
            }
        }

        // Format result string
        result += "Result " + (i+1) + ":" + total + " ";            
    }

    //Set the text view
    textView3.setText(result);
}

答案 1 :(得分:0)

您的实际代码适用于一次比较。 为什么不简单地将计算交集数量的部分提取到方法中,并为要执行的每个比较调用它?

public int countNbIntersection(String one, String two) { 

    String[] subStrings1 = one.split(", ");
    String[] subStrings2 = two.split(", ");

    Set<String> set1 = new HashSet<>();
    Set<String> set2 = new HashSet<>();

    for (String s : subStrings1) {
        set1.add(s);
    }
    for (String s : subStrings2) {
        set2.add(s);
    }
    set1.retainAll(set2);
    return set1.size();
} 

您可以调用它并生成预期的消息:

String reference = "1, 2, 3, 4, 5";
String other = "2, 3, 4, 5, 6";
String other2 = "3, 4, 5, 6, 7";

String firstCount = "Result 1 " + countNbIntersection(reference, other);
String secondCount = "Result 2 " +countNbIntersection(reference, other2);
String msg = firstCount  +  " " + secondCount;

答案 2 :(得分:0)

请使用输入StringCheck("1, 2, 3, 4, 5" , new String[]{"1, 5","1, 2, 3, 4, 5","7"});

尝试此代码
 public static void StringCheck(String one, String []two){
    String[] numbersOne = one.split(", ");//1 2 3 4 5
    String result = "";
    for (int i = 0; i < two.length; i++) {
        int counter = 0;
        String [] numbersTwo = two[i].split(", ");//2 3 4 5 6
        for (int j = 0; j < numbersTwo.length; j++) {
            for (int k = 0; k < numbersOne.length; k++) {
                if(numbersTwo[j].equals(numbersOne[k])){
                    counter++;
                    break;
                }
            }
        }
        result+="Result "+(i+1)+":"+counter+" ";
    }
        textView3.setText(result); 
}

输出结果为:Result 1:2 Result 2:5 Result 3:0