测试时Java中的数组消失

测试时Java中的数组消失,java,arrays,Java,Arrays,这对于学校来说是一个问题,我们应该创建一个方法,findCommonElements,在O(N)时间内找到任意数量数组的交集。我们只允许使用数组,所以我不能使用像ArrayList这样更方便的数据结构 我创建了自己的测试工具类,除了使用initBigIntArray方法初始化数组的测试外,其他测试都正常工作。我测试了initializearray函数,它确实创建了我所期望的数组,因此它似乎不是一个简单的一次性错误。问题是,要测试的结果数组始终显示为空,即使我将数组初始化为所有相同的值。有什么想法

这对于学校来说是一个问题,我们应该创建一个方法,findCommonElements,在O(N)时间内找到任意数量数组的交集。我们只允许使用数组,所以我不能使用像ArrayList这样更方便的数据结构

我创建了自己的测试工具类,除了使用initBigIntArray方法初始化数组的测试外,其他测试都正常工作。我测试了initializearray函数,它确实创建了我所期望的数组,因此它似乎不是一个简单的一次性错误。问题是,要测试的结果数组始终显示为空,即使我将数组初始化为所有相同的值。有什么想法吗

公共元素类:

import java.util.Arrays;
public class CommonElements {

    private static int comparisons = 0;
    public int getComparisons(){
        return comparisons;
    }
    public void clearComparisons(){
        comparisons = 0;
    }
    private void incrementComparisons(){
        comparisons++;
    }

    //workaround for not being able to use dynamic data structures
    public Comparable[] addArrayItem(Comparable[] targetArray, Comparable addItem){
        Comparable[] result = Arrays.copyOf(targetArray,targetArray.length+1);
        result[result.length-1] = addItem;
        return result;
    }

    public Comparable[] twoArrayIntersection(Comparable[] arrayOne, Comparable[] arrayTwo){
        int indexOne = 0;
        int indexTwo = 0;
        Comparable[] resultArray = new Comparable[0];
        //stops comparisons if one array is longer than another
        while(indexOne < arrayOne.length && indexTwo < arrayTwo.length){
            incrementComparisons();
            if(arrayOne[indexOne].equals(arrayTwo[indexTwo])){
                resultArray = addArrayItem(resultArray, arrayOne[indexOne]);
                indexOne++;
                indexTwo++;
            }
            else {
                incrementComparisons();
                if (arrayOne[indexOne].compareTo(arrayTwo[indexTwo]) < 0) {
                    indexOne++;
                } else {
                    incrementComparisons();
                    if (arrayTwo[indexTwo].compareTo(arrayOne[indexOne]) < 0) {
                        indexTwo++;
                    }
                }
            }
        }
        return resultArray;
    }

    public Comparable[] findCommonElements(Comparable[][] collection) {
        Comparable[] primaryArray = collection[0];
        for(int i = 1; i < collection.length; i++){
            primaryArray = twoArrayIntersection(primaryArray, collection[i]);
        }
        return primaryArray;
    }

}

并不是说你的阵列正在消失;它还在那儿!你只是看的地方不对。:)

看来你的

public Comparable[] findCommonElements(Comparable[][] collection) {
    Comparable[] primaryArray = collection[0]; <------------------------------Here
    for(int i = 1; i < collection.length; i++){
        primaryArray = twoArrayIntersection(primaryArray, collection[i]);
    }
    return primaryArray;
} 
只是看透和比较

Comparable[] col6 = initBigIntArray(0,1000); (stored in test2DArray3[1])

这是不一样的

很抱歉这些平淡的图像:

在findCommonElements中进行比较时,创建一个新数组,并将其值设置为
[0]
数组。当你们在两个数组的区间进行比较时,你们在测试它们是否相交,而它们并没有相交。如图所示,您期望的数组是2D数组的
[1]
标记部分

最后发生的事情是:在这两个带标记的数组上调用twoArrayIntersection,用带问号的箭头连接


没有一个元素是公共的,因此创建的新数组中没有元素,因此不会产生任何结果。

在问题案例中,您的方法
findCommonElements
会找到三个数组的交集,这显然是空的:

col6 <intersect> col7 = temp_result = {1000}

(col6 <intersect> col7) <intersect> col8 =
= temp_result <intersect> col8 =
= {1000} <intersect> {2000, ..., 3000} = {}
col6 col7=temp_result={1000}
(col6 col7)col8=
=临时结果col8=
= {1000}  {2000, ..., 3000} = {}

为什么只有使用initBigIntArray创建的数组才会出现这种情况,而不是我自己初始化的数组?很多实际的代码测试都是通过twoArrayIntersection函数进行的。primaryArray根据集合中的下一个数组进行测试,结果是新的primaryArray,依此类推。如果上述情况属实,那么我的其他结果也应该失败,对吧?不!它们将正常工作,因为它们只引用单个数组。我会更新更多信息,给我几分钟时间。:)哇!我不知道为什么我没有看到这个。我完全看错了。非常感谢。
    Comparable[] col6 = initBigIntArray(0,1000);
    Comparable[] col7 = initBigIntArray(1000,2000); <--- this
    Comparable[] col8 = initBigIntArray(2000,3000);
    Comparable[][] test2DArray3 = {col6,col7,col8}; <-- becomes test2DArray3[1]
    Comparable[] expected3 = {1000,2000};           <---related to test2DArray3[1]
Comparable[] primaryArray = collection[0];
Comparable[] col6 = initBigIntArray(0,1000); (stored in test2DArray3[1])
Comparable[] expected3 = {1000,2000};
col6 <intersect> col7 = temp_result = {1000}

(col6 <intersect> col7) <intersect> col8 =
= temp_result <intersect> col8 =
= {1000} <intersect> {2000, ..., 3000} = {}