简体   繁体   English

使用嵌套的for循环将String数组中的每个String与一个用户输入字符串进行比较

[英]Comparing each String in String array to one string of user input using nested for loops

I am trying to compare two strings and sum the differences of their chars. 我正在尝试比较两个字符串并求和它们的字符的差异。 One string is a user input (input) and the other is from an array of strings (binary_patterns_array). 一个字符串是用户输入(输入),另一个来自字符串数组(binary_patterns_array)。

I would like to compare each string in the array to the input and place the sum of their differences into another int array. 我想将数组中的每个字符串与输入进行比较,并将它们的差之和放入另一个int数组中。 I have another method that iterates through the int array to find the smallest value which will give me the index of closest matching pattern. 我有另一种方法遍历int数组以找到最小值,该最小值将给我最接近的匹配模式的索引。 Here's the code: 这是代码:

public static int[] difference(String input, String[] binary_patterns_array){
    int[] difference_array = new int[32];
    String  binary_pattern = "";
    int count = 0;
    for(int i = 0; i < binary_patterns_array.length; i++){
        binary_pattern = binary_patterns_array[i];
        for(int j = 0; j < input.length(); j++){
            if (binary_pattern.charAt(j) != input.charAt(j)){
                count += 1;
            }
         difference_array[i] = count;
        }
    }
    return difference_array;
}

This doesn't work but I'm not exactly sure why. 这不起作用,但我不确定为什么。 The two loops do exactly what I want them to, but my if statement or the way i'm summing the differences isn't doing what I had hoped. 这两个循环完全可以实现我想要的功能,但是我的if语句或我总结差异的方式并没有达到我的期望。 Can anyone see any blatant mistakes? 谁能看到任何公然的错误? Thank you! 谢谢!

You need to reset count in every pattern iteration: 您需要在每个模式迭代中重置count

public static int[] difference(String input, String[] binary_patterns_array){
    int[] difference_array = new int[32];
    String  binary_pattern = "";
    for(int i = 0; i < binary_patterns_array.length; i++){
        int count = 0; // moved inside the loop so it is 0 at the beginning of each pattern
        binary_pattern = binary_patterns_array[i];
        for(int j = 0; j < input.length(); j++){
            if (binary_pattern.charAt(j) != input.charAt(j)){
                count += 1;
            }
        }
        difference_array[i] = count; // moved because it is cleaner outside the inner loop
    }
    return difference_array;
}

Multiple issues 多个问题

  1. count should be reset every time 每次应重新设置计数
  2. Array size is hardcoded. 数组大小是硬编码的。 It should be set as the size of binary_patterns_array 应该将其设置为binary_patterns_array的大小
  3. difference_array[i] should be set outside the inner loop. Difference_array [i]应该在内部循环之外设置。
  4. No need to create temporary variable. 无需创建临时变量。 It is just a memory overhead. 这只是内存开销。

     public static int[] difference(String input, String[] binary_patterns_array) { int[] difference_array = new int[binary_patterns_array.length]; for (int i = 0; i < binary_patterns_array.length; i++) { int count = 0; for (int j = 0; j < input.length(); j++) { if (binary_patterns_array[i].charAt(j) != input.charAt(j)) { count++; } } difference_array[i] = count; } return difference_array; } 

Initialize count above inner loop and put difference_array[i] = count; 在内部循环上方初始化count,然后将difference_array[i] = count; out of inner loop. 摆脱内循环。

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

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