【问题标题】:How to compare elements between two arrays?如何比较两个数组之间的元素?
【发布时间】:2021-08-08 04:58:44
【问题描述】:

我的程序有一个布尔数组,它在比较两个数组之间的元素时返回 true,即使它们的值不相等。如何将我的 compArray 方法更正为仅在元素彼此相等时才返回 true 的位置?

public static boolean compArray(double[] a, double[] b, int n) {
    for (int i = 0; i < a.length; i++) {
        if (a.length == b.length) {
            if (a[i] == b[i]) {
                return true;
            } else return false;
        }
    }
    return false;
}
public static void main(String[] args) {
    double[] a = new double[5];
    double[] b = new double[5];
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the amount of values: ");
    int n = sc.nextInt();
    for (int i = 0; i < n; i++) {
        System.out.println("Please enter values for array a: ");
        a[i] = sc.nextDouble();
        System.out.println("Please enter values for array b: ");
        b[i] = sc.nextDouble();
    }

    boolean match = compArray(a, b, n);
    for (int i = 0; i < n; i++) {
        if (compArray(a, b, n)) {
            System.out.println("A " + a[i] + " B " + b[i] + " true");
            System.out.println();
        } else {
            System.out.println("A " + a[i] + " B " + b[i] + " false");
            System.out.println();
        }
    }
    sc.close();
}

【问题讨论】:

  • 现在,由于您在for 循环内有一个return 语句,所以return 第一次执行时,整个函数将结束。这意味着如果数组中的第一个元素相等,该函数将立即返回true

标签: java arrays boolean


【解决方案1】:

不要重新发明轮子。有一个库函数:

public static boolean compArray(double[] a, double[] b, int n) {
     return Arrays.equals(a, 0, n, b, 0, n);
}

Arrays.equals​(double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)

【讨论】:

    【解决方案2】:

    如果您只想比较数组中的 2 个元素,则不需要函数。你传入n,但你可以检查a[n] == b[n]。我假设你想要的是一个比较整个数组的函数。

    不要在第一个元素相等时返回true 并尝试在最后返回false,而是扫描整个数组以查找不相等的元素。一旦到达不相等的对,您就知道数组不相等,您可以返回false。如果没有一个元素不相等,则所有元素都相等,您可以返回true

    public static boolean compArray (double[] a, double[] b)
    {
        for (int i = 0; i < a.length; i++)
        {
            if (a.length == b.length) {
                if (a[i] != b[i])
                {
                    return false;
                }
            }
        }
        return true;
    }
    

    您可能想了解breakreturn 之间的区别。

    您可以做的另一件事是将长度检查移到循环之外,如下所示:

    public static boolean compArray (double[] a, double[] b)
    {
        // immediately exit if the lengths are not equal
        if (a.length != b.length)
        {
            return false;
        }
    
        // loop through and exit once an unequal pair is found
        for (int i = 0; i < a.length; i++)
        {
            if (a[i] != b[i])
            {
                // returns from the whole function, not just the for loop
                return false;
            }
        }
    
        // if this code is reached, the lengths must have been equal and there were no pairs that didn't match, so the arrays must be completely equal
        return true;
    }
    

    这使代码更具可读性,并且不需要在每次循环迭代时检查长度条件——只需在函数开始时检查一次。

    【讨论】:

      【解决方案3】:

      以下是比较两个双精度数组是否相等的方法:

      public static boolean compArray (double[] a, double[] b, int n) {
          //if the arrays dont have the same length, then they are not equals
          if (a.length != b.length) {
              return false;
          }
      
          for (int i = 0; i < a.length; i++) {
                  //if one element at array a is different than the other at array b, 
                  //at the same index, then the arrays are not equals
                  if (a[i] != b[i]) {
                      return false;
                  }
              }
          }
          
          //otherwise, the two arrays are equals
          return true;
      }
      

      【讨论】:

        【解决方案4】:

        当您应该传递“i”时,您将传递“n”作为要比较的索引。您也可以简化该方法,因为您的 main 函数中有一个 for 循环:

        public static boolean compArray (double[] a, double[] b, int i)
          {
            if(a[i] == b[i]){
              return true;
            }
            return false;
          }
        

        在主...

        for (int i=0; i < n; i++){
              if (compArray(a, b, i))
        

        示例输入: 2 1 1 3 4

        示例输出:A 1.0 B 1.0 真,B 3.0 B 4.0 假

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-15
          • 1970-01-01
          • 2011-01-13
          相关资源
          最近更新 更多