【问题标题】:Comparing the values of two arrays [duplicate]比较两个数组的值[重复]
【发布时间】:2020-11-02 21:26:51
【问题描述】:

您好,我在编写代码方面需要帮助! 所以我必须编写 java 代码 1. 判断两个数组是否相等(相同索引位置的相同值) 2. 如果它们不相同,那么我们想知道有多少值不同以及它们之间的最大差异两个值。

这是两个数组:

double[] Array1 = {10.2, 2.7, 6.4};  
double[] Array2 = {5.7, 4.0, 2.7}; 

这是我迄今为止所做和尝试过的,它可能是错误的,所以请告诉我正确的解决方案以及如何继续解决问题:

for (i=0; i<Array1.length; i++) {
  for( i=0; j<Array2.length; j++) {

    if (Array1 [i] == Array2 [j]) {
      

      System.out.println ("Arrays are equal");

    }
    else {
      if (Array1 [i] != Array2 [j] ) {
      System.out.println ("Arrays are not equal" );

【问题讨论】:

  • 一般来说,我们这里只修复bug。如果您的代码没有问题,我们不会“修复”它。 “请告诉我这是否有效”不会飞。运行并测试您自己的代码。如果您发现问题,请发布您遇到的问题。如果您没有发现问题,我们无法为您“修复”它。
  • 使用== 比较浮点数通常是个坏主意。考虑改用Double.compare(...)

标签: java arrays


【解决方案1】:

考虑到最大的差异是在相同的位置值上计算的。

void compare(double[] Array1, double[] Array2) {
        int countDifferent = 0;
        double largestDiff = 0;
        int i= 0;
        for(; i < Array2.length && i < Array1.length; i++) {
            if(Array2[i] != Array1[i]) {
                countDifferent++; 
                if(Math.abs(Array1[i] - Array2[i]) > largestDiff) {
                    largestDiff = Math.abs(Array1[i] - Array2[i]);
                }
            }
        }
        if(i < Array1.length)
            countDifferent += Array1.length - i;
        if(i < Array2.length)
            countDifferent += Array2.length - i;
        if(countDifferent > 0) {
            System.out.println("Differnt Count: " + countDifferent);
            System.out.println("Largest Diff: " + largestDiff);
        }else {
            System.out.println("Arrays are equal..");
        }
    }

【讨论】:

    【解决方案2】:

    首先,检查长度,如果长度相同,比较相同索引处的值,如果有,则计算不相等的值的数量

    这是我为您推荐的代码,您可以根据自己的确切需要进行更改。

    int cmp = 0;
    if(Array1.length != Array2.length){
       System.out.println("Arrays are not equal - not the same length -");
       return;
    }else
           for (i=0; i<Array1.length; i++)
               if (Array1[i] != Array2[i])
                    cmp++;    
    if(cmp == 0) System.out.println("the two arrays are equalled");
    else System.out.println("the two arrays are not equalled with " + cmp + " different values.");
    

    【讨论】:

    • 不回答 #2。
    • 我回答了#2 - 有多少值不同 - 我没有回答它的最后一部分 - 最大的差异 - 因为我认为他关于方法的问题不是关于编写完整代码的问题。他只需添加两到三行简单的行即可找到最大的差异,这就是为什么我说“您可以根据您的确切需要更改它”
    【解决方案3】:

    你做得很好,不需要 2 个循环,也不需要额外的比较
    你能做到这样的功能吗? (伪代码)

    @Test   
    public void compare(){
    
    double[] array1 = {10.2, 2.7, 6.4};  
    double[] array2 = {5.7, 4.0, 2.7};
    
    //double[] array1 = {10.2, 2.7, 6.4};
    //double[] array2 = {10.2, 2.7, 6.4};
    
    
    int howManyDiffarent =0;
    double largestDiffarenc = 0;
    
      if(array1.length != array2.length){
          System.out.println("Arrays are not equal");
          return;
       }
    
    for (int i=0; i<array1.length; i++) { //run over the length    
        if (Double.compare(array1 [i],array2 [i]) > 0) { //compare content        
            howManyDiffarent++;                           //count differences   
            
            double compare = Math.abs(array1 [i]) - Math.abs(array2 [i]);
            if(compare > largestDiffarenc) { //store the largest
                largestDiffarenc = compare;
            }
        }
     }
    if(howManyDiffarent==0) {                       
        System.out.println("Arrays are equal");//no differences found 
    }else {                               
        System.out.println("Arrays are not equal, there are:" +howManyDiffarent + " differences, largest diffrence is:" +largestDiffarenc);
        
    }
    

    }

    【讨论】:

    • 不编译也不回答#2。
    • 更新了,可以去掉Test注解来运行或者使用junit。
    • 结果不正确并抛出异常。
    • 有什么异常?
    • ArrayIndexOutOfBoundsException,当array1array2 长时。 --- 当array1 值小于array2 值时,结果不正确。 --- 当值具有不同符号时,结果不正确,例如-2.74.0 之间的区别是 6.7,而不是 -1.3
    【解决方案4】:

    如果你的数组长度相同,我认为你可以这样做。

    我没有运行代码没有安装java所以可能有错误。

    double[] Array1 = {10.2, 2.7, 6.4};  
    double[] Array2 = {5.7, 4.0, 2.7}; 
    
    boolean is_same_length=false;
    boolean is_same = true;
    int total_different_value_count = 0;
    double difference = 0;
    
    if(Array1.length == Array2.length)
    {
        is_same_length = true;
    }
    else
    {
        is_same_length = false;
        is_same = false;
    }
    
    if(is_same_length)
    {
        for (int i=0; i<Array1.length; i++) {
            if(Array1[i] != Array2[i])
            {
                is_same = false;
                total_different_value_count++;
                if(Array1[i] - Array2[i] > difference)
                {
                    difference = Array1[i] - Array2[i];
                }
    
                if(Array2[i] - Array1[i] > difference)       //Edit missed this
                {
                    difference = Array2[i] - Array1[i];
                }
            }
        }
    }
    
    if(is_same == true)
    {
        System.out.println ("Arrays are equal");
    }
    else
    {
        System.out.println ("Arrays are not equal");
        System.out.println ("Count of values that are different :" + total_different_value_count);
        System.out.println ("Largest difference between two values are :" + difference);
    }
    

    【讨论】:

    • 无法编译:i 未定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 2013-08-02
    相关资源
    最近更新 更多