两个数组的交集出现 IndexOutOfBound 错误

Intersection of Two Arrays getting IndexOutOfBound Error

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

输入: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);

    }

}

我可以建议使用像 Set 这样的额外数据结构来解决问题的更简单的方法吗

  1. 处理 array1 并将所有元素添加到名为 array1UniqSet 的集合中
  2. 之后遍历 array2 中的每个元素并检查 array1UniqSet 是否包含该元素: -> 如果元素存在,将它们添加到某个输出列表 -> 如果不向前移动 array1
  3. 中的下一个元素
  • 我注意到的一个问题是您没有在 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 值。正确的技术是用相同的方法完成,以减轻进一步处理的负担。

  • 在下面的循环中,看我的评论。
// 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);
    }
}