【问题标题】:How can I compare one string elements to array of strings elements?如何将一个字符串元素与字符串元素数组进行比较?
【发布时间】:2018-04-09 16:25:05
【问题描述】:

我有一个函数可以比较 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

【问题讨论】:

    标签: java android arrays string function


    【解决方案1】:

    请尝试输入以下代码: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

    【讨论】:

      【解决方案2】:

      这有点粗糙,但应该可以:

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

      【讨论】:

        【解决方案3】:

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

        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;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-17
          • 1970-01-01
          • 2019-05-02
          • 2020-05-29
          • 1970-01-01
          • 2012-11-22
          相关资源
          最近更新 更多