【问题标题】:Comparing two arrays. Wrong return value (1)比较两个数组。返回值错误 (1)
【发布时间】:2015-01-19 17:51:30
【问题描述】:

我做了这个方法,比较两个数组的数,然后返回有多少个数相等,但是不管有多少个数相等,这个方法每次都返回值1。 (两个数组长度相同)。

public static void main(String[] args) {
    int a []  = {1, 4, 6, 7, 8, 10, 13};
    int b []  = {1, 2, 3, 4, 5, 6, 7};

    equal(a,b);

}


public static int equal(int[] a, int[] b){
    int j = 0;
    for(int i = 0; i< a.length-1;i++){

        if(a[i] == b[i]){
            j++;
        }
    }
    System.out.println(j);
    return j;
}

【问题讨论】:

  • 这些数组完全正确。

标签: java arrays methods return main


【解决方案1】:

您的代码在同一索引处查找相等的数字。

有几种方法可以找到交叉点的大小。

一个简单但 O(m*n) 的实现是为 a 的每个元素迭代 b 的所有元素。

如果数组已排序,您可以为两个数组使用单独的索引,当它们不再匹配时推进每个索引。这将是 O(m+n)。 (如果它们没有排序,你可以先对它们进行排序,成本为 O(m log m + n log n )。

如果每个数组没有重复的成员,另一种方法是根据集合差的大小来计算交集的大小。这方面的一个例子是http://ideone.com/6vLAfn。关键部分是将每个数组转换为一个集合,并通过从另一个集合中删除一个集合来确定有多少成员是共同的。

 int aSizeBefore = setA.size();
 setA.removeAll( setB );
 int aSizeAfter = setA.size();
 return aSizeBefore - aSizeAfter;

【讨论】:

    【解决方案2】:

    如果要检查数组 a 中的任何单个数字是否也在数组 b 中,则应使用嵌套的 for 循环。

    例如

    int numMatches = 0;
    for (int i = 0; i < a.length; ++i)
    {
        for (int j = 0; j < b.length; ++j)
        {
            if (a[i] == b[j])
                ++numMatches; //Naive, as obviously if the same number appears twice in a it'll get counted twice each time it appears in b.
        }
    }
    

    当前代码只检查相同索引匹配的元素,即

    1 == 1 // Yes, increment j
    4 == 2 // Nope
    6 == 3 // Nope
    7 == 4 // Nope
    8 == 5 // Nope
    10 == 6 // Nope
    13 == 7 // Nope
    

    【讨论】:

      【解决方案3】:

      具有相同值的元素可能位于不同的索引中。假设数组已排序,您可以编写如下:

      public static int equal(int[] a, int[] b) {
          int count = 0;
          for(int i = 0; i < a.length - 1; i++) {
              for(int j = 0; i < b.length - 1; j++) {
      
                  if (a[j] < b[j]) {
                      // we came to the part where all elements in b are bigger 
                      // than our selected element in a
                      break;
                  }
                  else if (a[j] == b[j]) {
                      count++;
                  }
              }
           }
           System.out.println(count);
           return count;
      }
      

      如果你不能保证数组是有序的,你可以删除 if-block 并从循环中删除 else-if 的 else。

      【讨论】:

        【解决方案4】:

        如果您想知道两个数组中有多少个数字并且可以保证它们是有序的,您应该尝试以下操作:

        public static int equal(int[] a, int[] b) {
                int j, result = 0;
                int lastFound = 0;
                for (int i = 0; i < a.length - 1; i++) {
                    for (j = lastFound; j < b.length; j++) {
                        if (a[i] == b[j]) {
                            result++;
                            lastFound = j;
                            break;
                        } else {
                            if (a[i] < b[j]) break;
                        }
                    }
                }
                return result;
        
            }
        

        使用变量lastFound 将加快您的循环,但它只有在数组是有序的情况下才有用,如您的示例所示。

        【讨论】:

          猜你喜欢
          • 2014-12-08
          • 2015-07-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-15
          • 2021-09-06
          相关资源
          最近更新 更多