簡體   English   中英

如何將一個字符串元素與字符串元素數組進行比較?

[英]How can I compare one string elements to array of strings elements?

我有一個比較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

這有點粗糙,但這應該可以工作:

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);
}

您的實際代碼只能進行一次比較。 為什么不簡單地將計算交叉點數量的部分提取到方法中,並為要執行的每個比較調用它呢?

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;

請通過輸入嘗試以下代碼: 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM