【问题标题】:Intersection of Two Arrays getting IndexOutOfBound Error两个数组的交集得到 IndexOutOfBound 错误
【发布时间】:2020-07-09 17:02:34
【问题描述】:

程序是:找到两个数组的交集(公共元素)。

输入:arr1 -> {10.45, 14.0, 18.35, 88.88, 54.10, 18.35}

arr2 -> {17.20, 13.30, 10.45, 18.35, 84.33, 13.30}

输出:10.45, 18.35

方法签名:double[] getIntersectionOfArray(double[] arr1, double[] arr2)

方法:删除两个数组的重复元素,比较两个数组并将共同值分配给第三个数组 问题:只要 m 调用第二个数组的方法,它就会给出索引出界数组。 有人能解释一下为什么会这样吗?

限制:不要使用收集方法

导入 java.util.Arrays;

public class Test1 {
    int count = 0;
    int resultcount = 0;

    void findIntersectionOfArray(double[] array1, double[] array2) {
        double[] tempArray = new double[array1.length ]; //12
        
        double[] tempArray1 = new double[array2.length];
        // Calling Method to find Unique Array
         tempArray = uniqueArray1(array1);
         tempArray1 = uniqueArray1(array2);
         

        // Removing 0.0 Extra value & Assigning array1 Unique value in array result 1
        double[] resultArray1 = new double[count];
        for (int index = 0; index < resultArray1.length; index++) {
            resultArray1[index] = tempArray[index];
        }

        // Removing 0.0 Extra value & Assigning array 2 Unique value in array result 2
        double[] resultArray2 = new double[count];
        for (int index = 0; index < resultArray2.length; index++) {
            resultArray2[index] = tempArray[index];
        }
        //System.out.println("Unique Array 1 : " + Arrays.toString(resultArray1));
        //System.out.println("Unique Array 2 : " + Arrays.toString(resultArray2));

        // Calling Method to Get Size of an intersection array
        int size = getSizeOfAnArray(array1, array2);
        double[] intersectArray = new double[size];

        // Finding Common Elements between between 2 Arrays
        boolean flag = true;
        for (int outerindex = 0; outerindex < resultArray1.length; outerindex++) {
            flag = true;
            for (int innerindex = 0; innerindex < resultArray2.length; innerindex++) {
                if (resultArray1[outerindex] == resultArray2[innerindex]) {
                    flag = false;
                }
            }
            if (flag == false) {
                intersectArray[outerindex] = resultArray1[outerindex];
                count++;
            }
        } // O/p : [10.45, 0.0, 18.35, 0.0, 0.0, 0.0]
        //System.out.println("Intersection array " + Arrays.toString(intersectArray));

        // Printing Final Array Value getting 0.0 as well
        for (int index = 0; index < intersectArray.length; index++) {
            System.out.print(intersectArray[index] + " ,");
        }
    }

    // Get Size of an intersectArray
    int getSizeOfAnArray(double[] array1, double[] array2) {
        int size = 0;
        if (array1.length < array2.length)
            size = array1.length;
        else
            size = array2.length;
        return size;
    }

    // Method Unique Array will give Array1 Unique element
    double[] uniqueArray1(double[] givenArray) {
        double[] tempArray = new double[givenArray.length]; // 6
        boolean isNumberPresent = true;
        for (int outerindex = 0; outerindex < givenArray.length; outerindex++) {
        
            isNumberPresent = true;
            for (int innerindex = 0; innerindex < givenArray.length; innerindex++) {
                if ((givenArray[outerindex] == tempArray[innerindex])) {
                    isNumberPresent = false;
                }
            }
            if (isNumberPresent) {
                tempArray[count] = givenArray[outerindex];
                count++;
            }
        }
        return tempArray; // [10.45, 14.0, 18.35, 88.88, 54.1,0.0]
    }

    
    public static void main(String[] args) {
        Test1 test = new Test1();
        double[] array1 = { 10.45, 14.0, 18.35, 88.88, 54.10, 18.35 };
        double[] array2 = { 17.20, 13.30, 10.45, 18.35, 84.33, 13.30 };
        test.findIntersectionOfArray(array1, array2);

    }

}

【问题讨论】:

  • 哪一行是异常?
  • 上传的更新代码@QBrute @Harpistry 在第 81 行出现错误tempArray[count] = givenArray[outerindex];

标签: java arrays for-loop intersection


【解决方案1】:
  • 我注意到的一个问题是您没有在uniqueArray1 中重置count。我会在方法开始时将其设置为 0。
  • 您应该将count 值复制到retArray,如下所示。
        double[] retArray = new double[count];
        for (int i = 0; i < count; i++) {
            retArray[i] = tempArray[i];
        }
        return retArray; 

这将消除任何挥之不去的 0.0 值。正确的技术是用相同的方法完成,以消除进一步处理的负担。

  • 在以下循环中,请参阅我的 cmets。
// need to reset count to 0
       count = 0;
        // Finding Common Elements between between 2 Arrays
        boolean flag = true;
        for (int outerindex = 0; outerindex < resultArray1.length;
                outerindex++) {
            flag = true;
            for (int innerindex = 0; innerindex < resultArray2.length;
                    innerindex++) {
                if (resultArray1[outerindex] == resultArray2[innerindex]) {
                    flag = false;  
                    // might as well get out of this loop since flag will never 
                    // be true.                    
                    break;
                }
            }
            if (flag == false) {
// important to assign to location count (at the beginning of the
// array).
                intersectArray[count] = resultArray1[outerindex];
                count++;
            }
        }

  • 打印值时,使用计数,而不是数组长度。
        for (int index = 0; index < count; index++) {
            System.out.print(intersectArray[index] + " ,");
        }

这是完整的程序。一些名称略有变化。

import java.util.Arrays;

public class Test1 {
    
    void findIntersectionOfArray(double[] array1, double[] array2) {
        // get unique elements and return arrays.
        double[] resultArray1 = uniqueArray1(array1); //12
        double[] resultArray2 = uniqueArray1(array2);
        
        // initialize temporary result array
        double[] tempResultArray = new double[array1.length];
        // Finding Common Elements between between 2 Arrays
        
        boolean flag = true;
        int count = 0;
        for (int outerindex = 0; outerindex < resultArray1.length; outerindex++) {
            flag = true;
            for (int innerindex = 0; innerindex < resultArray2.length; innerindex++) {
                if (resultArray1[outerindex] == resultArray2[innerindex]) {
                    flag = false;
                    break; // break out if false since it won't go back to true
                }
            }
            // add element to temp result array at count location
            if (flag == false) {
                tempResultArray[count] = resultArray1[outerindex];
                count++;
            }
        } 
        
        // prepare to fill final result array of size count
        double[] finalResultArray = new double[count];
       
        for (int index = 0; index < count; index++) {
            finalResultArray[index] = tempResultArray[index];
        }
        // print the array.
        System.out.println(Arrays.toString(finalResultArray));
    }

    // Method Unique Array will give Array1 Unique element
    double[] uniqueArray1(double[] givenArray) {
        int count = 0;
        double[] tempArray = new double[givenArray.length]; // 6
        boolean isNumberPresent = true;
        for (int outerindex = 0; outerindex < givenArray.length; outerindex++) {
        
            isNumberPresent = true;
            for (int innerindex = 0; innerindex < givenArray.length; innerindex++) {
                if ((givenArray[outerindex] == tempArray[innerindex])) {
                    isNumberPresent = false;
                }
            }
            if (isNumberPresent) {
                tempArray[count] = givenArray[outerindex];
                count++;
            }
        }
        
        // only copy valid array elements
        double[] returnArray = new double[count];
        for (int i = 0; i < count; i++) {
            returnArray[i] = tempArray[i];
        }
        return returnArray; 
    }

    
    public static void main(String[] args) {
        Test1 test = new Test1();
        double[] array1 = { 10.45, 14.0, 18.35, 88.88, 54.10, 18.35 };
        double[] array2 = { 17.20, 13.30, 10.45, 18.35, 84.33, 13.30 };
        test.findIntersectionOfArray(array1, array2);
    }
}

【讨论】:

  • 我不想收藏
  • 当我发现一些新问题时,我制作了一些新的 cmets。您的程序似乎有效。
  • 使用此代码无法正常工作double[] retArray = new double[count]; for (int i = 0; i &lt; count; i++) { retArray[i] = tempArray[i]; } return retArray;
  • @PalakSoni 对于任何混淆,我深表歉意。我可能没有很好地解释它或犯了一个错误。我在最终版本中进行了一些修改和解释性 cmets。一个主要想法是使用本地值count,而不是在方法之间共享一个。请使用您认为合适的任何内容。
【解决方案2】:

我能否建议一种更简单的方法来解决这个问题,使用像 Set 这样的额外数据结构

  1. 处理 array1 并将所有元素添加到名为 array1UniqSet 的 Set 中
  2. 之后遍历 array2 中的每个元素并检查 array1UniqSet 是否包含该元素: -> 如果元素存在,则将它们添加到某个输出列表 -> 如果不向前移动 array1 中的下一个元素

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-25
    • 2016-03-23
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 2014-07-04
    相关资源
    最近更新 更多