对于我们应该创建方法的学校来说,这是一个问题,findCommonElements在O(N)时间内找到任意数量的数组的交集。我们只允许使用Arrays,因此我无法使用更方便的数据结构,如ArrayList。
我创建了自己的测试工具类,我的测试都在工作,除了我使用方法的那个,initBigIntArray来初始化数组。我测试了我的初始化数组函数,它确实创建了我期待的数组,所以它似乎不是一个简单的错误。问题是我要测试的结果数组总是显示为空,即使我将数组初始化为全部相同的值。有什么想法吗?
CommonElements类:
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;
}
}
TestHarness Class:
import java.util.Arrays;
public class TestHarness {
public static void main(String args[]){
CommonElements testClass = new CommonElements();
Comparable[] col1 = {0,1,2,3,4,5,6,7,8,9,10};
Comparable[] col2 = {10,11,12,13,14,15,16,17,18,19,20};
Comparable[][] test2DArray1 = {col1, col2};
Comparable[] expected1 = {10};
testResults(testClass,test2DArray1,expected1);
Comparable[] col3 = {5,8,12,13,15,15,17,18,19,20};
Comparable[] col4 = {0,3,5,8,15,15,20,25,27,31};
Comparable[] col5 = {1,5,15,15,18,20,27,100};
Comparable[][] test2DArray2 = {col3,col4,col5};
Comparable[] expected2 = {5,15,15,20};
testResults(testClass,test2DArray2,expected2);
//this is the only one I am having problems with
Comparable[] col6 = initBigIntArray(0,1000);
Comparable[] col7 = initBigIntArray(1000,2000);
Comparable[] col8 = initBigIntArray(2000,3000);
Comparable[][] test2DArray3 = {col6,col7,col8};
Comparable[] expected3 = {1000,2000};
testResults(testClass,test2DArray3,expected3);
}
public static Comparable[] initBigIntArray(int start, int end){
int size = (end - start) + 1; //10 - 3 = 7 but array from 3 through 10 has 8 values
Comparable[] result = new Comparable[size];
for(int i = 0; i < size; i++){
result[i] = start;
start++;
}
return result;
}
public static void testResults(CommonElements testClass, Comparable[][] test2DArray, Comparable[] expected){
Comparable[] result = testClass.findCommonElements(test2DArray);
System.out.print("Expected: ");
for(Comparable i: expected){
System.out.print(i + ",");
}
System.out.println();
System.out.print("Result: ");
for(Comparable i: result){
System.out.print(i + ",");
}
System.out.println();
System.out.print("Pass/Fail: ");
if(Arrays.deepEquals(expected,result)){
System.out.print("PASS");
}
else{
System.out.print("FAIL");
}
System.out.println();
int nValues = 0;
for(Comparable[] column: test2DArray){
nValues = nValues + column.length;
}
System.out.println("NValues: " + nValues);
System.out.println("Comparisons: " + testClass.getComparisons());
System.out.println();
}
}
以下是我得到的结果。注意最后一个我的结果数组是空的。
Expected: 10,
Result: 10,
Pass/Fail: PASS
NValues: 22
Comparisons: 21
Expected: 5,15,15,20,
Result: 5,15,15,20,
Pass/Fail: PASS
NValues: 28
Comparisons: 54
Expected: 1000,2000,
Result:
Pass/Fail: FAIL
NValues: 3003
Comparisons: 2057
答案 0 :(得分:1)
并不是你的阵列正在消失;它还在那里!你只是在寻找合适的区域。 :)
看来你的
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;
}
仅搜索2D数组的第一部分
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]
然而,您需要的数组是1,而不是[0],因为1包含1000到2000之间的所有值,这就是您要搜索的内容。
简而言之:
Comparable[] primaryArray = collection[0];
只查看并比较
Comparable[] col6 = initBigIntArray(0,1000); (stored in test2DArray3[1])
到
Comparable[] expected3 = {1000,2000};
哪些不一样。
在findCommonElements中进行比较时,您将创建一个新数组,并将其值设置为[0]
数组。当您在twoArrayIntersection中进行比较时,您正在测试它们是否相交,而不是它们。如图所示,您期望的数组是2D数组的[1]
indice部分。
所以最终发生的事情是这样的:你在那两个标记的数组上调用twoArrayIntersection,用带有问号的箭头连接。
没有一个元素是常见的,所以创建一个没有元素的新数组,没有结果。
答案 1 :(得分:1)
在问题情况下,您的方法findCommonElements
找到三个数组的交集,显然是空的:
col6 <intersect> col7 = temp_result = {1000}
(col6 <intersect> col7) <intersect> col8 =
= temp_result <intersect> col8 =
= {1000} <intersect> {2000, ..., 3000} = {}