如何初始化数组以便稍后在测试方法中使用(Java)
我比较两个数组中的元素并打印这些元素,加上比较的次数 在“findCommonElements”方法中,我不能有空数组,或者我得到一个NullPointerException(当然)错误 我不能只声明它们,否则会出现“变量可能尚未初始化”错误 我必须使用这些阵列的多个版本进行测试,但我不确定如何进行测试。程序只读取数组的第一个实例 代码:如何初始化数组以便稍后在测试方法中使用(Java),java,arrays,testing,variable-declaration,Java,Arrays,Testing,Variable Declaration,我比较两个数组中的元素并打印这些元素,加上比较的次数 在“findCommonElements”方法中,我不能有空数组,或者我得到一个NullPointerException(当然)错误 我不能只声明它们,否则会出现“变量可能尚未初始化”错误 我必须使用这些阵列的多个版本进行测试,但我不确定如何进行测试。程序只读取数组的第一个实例 代码: import java.util.*; public class CommonElements { private static int comparis
import java.util.*;
public class CommonElements {
private static int comparisons = 0;
public static void main(String[] args) {
new CommonElements().firstTest();
//new CommonElements().secondTest();
//new CommonElements().thirdTest();
}
public void setComparisons(int comparisons) {
this.comparisons = comparisons;
}
public int getComparisons() {
return comparisons;
}
public static Comparable[] findCommonElements(Comparable[][] collections) {
Comparable[] arr1 = {'A', 'B', 'C', 'D'};
Comparable[] arr2 = {'C', 'D', 'E', 'F', 'G'};
Comparable[] hashArray;
Comparable[] searchArray;
if(arr1.length < arr2.length) {
hashArray = arr1;
searchArray = arr2;
}
else {
hashArray = arr2;
searchArray = arr1;
}
HashSet<Comparable> intersection = new HashSet<>();
HashSet<Comparable> hashedArray = new HashSet<>();
for(Comparable element : hashArray) {
hashedArray.add(element);
}
for(Comparable element : searchArray) {
if(hashedArray.contains(element)) {
intersection.add(element);
comparisons++;
}
}
return intersection.toArray(new Comparable[0]);
}
public void firstTest() {
Comparable[] array1 = {'A', 'B', 'C', 'D', 'E'};
Comparable[] array2 = {'E', 'F', 'G', 'H'};
Comparable[][] collections = {array1, array2};
Comparable[] commonStuff = CommonElements.findCommonElements(collections);
System.out.println("First Test");
System.out.println("Comparisons: " + getComparisons());
System.out.print("Common Elements: ");
for(Comparable element : commonStuff) {
System.out.print(element.toString() + " ");
}
System.out.println();
}
/*
public void secondTest() {
Comparable[] array1 = {'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};
Comparable[] array2 = {'D', 'E', 'F', 'G', 'H'};
Comparable[][] collections = {array1, array2};
Comparable[] commonStuff = findCommonElements(collections);
System.out.println("Second Test");
System.out.println("Comparisons: " + getComparisons());
for(Comparable element : commonStuff) {
System.out.print(element.toString() + " ");
}
System.out.println();
}
public void thirdTest() {
Comparable[] array1 = {'1', '2', '3', '3'};
Comparable[] array2 = {'3', '2', '4', '5', '6'};
Comparable[][] collections = {array1, array2};
Comparable[] commonStuff = findCommonElements(collections);
System.out.println("Third Test");
System.out.println("Comparisons: " + getComparisons());
for(Comparable element : commonStuff) {
System.out.print(element.toString() + " ");
}
System.out.println();
}
*/
}
注意
是的,我在findCommonElements方法中有一个未使用的参数。这是一个要求,我不知道如何将2D阵列合并到这个中……至少还没有
是的,阵列的类型是可比较的。这是另一个要求
此外,如果我的任何逻辑在此代码中不正确,我深表歉意。我现在只是想解决这个问题。要声明数组,应该使用以下语法:
// Type[] array = new Type[arrayLenght];
例如,如果要创建长度10的可比数组,则应执行以下操作:
Comparable[] myArray = new Comparable[10];
要声明数组,应使用以下语法:
// Type[] array = new Type[arrayLenght];
例如,如果要创建长度10的可比数组,则应执行以下操作:
Comparable[] myArray = new Comparable[10];
而不是
public static Comparable[] findCommonElements(Comparable[][] collections) {
Comparable[] arr1 = {'A', 'B', 'C', 'D'};
Comparable[] arr2 = {'C', 'D', 'E', 'F', 'G'};
写下以下内容(假设您总是得到2个元素):
然后您的测试开始工作。而不是
public static Comparable[] findCommonElements(Comparable[][] collections) {
Comparable[] arr1 = {'A', 'B', 'C', 'D'};
Comparable[] arr2 = {'C', 'D', 'E', 'F', 'G'};
写下以下内容(假设您总是得到2个元素):
然后您的测试开始工作。您可以检查您的参数是否为null,如果其中任何参数为null,您可以返回一个空的Comparablearray(在空情况下这是真实的)
公共静态可比[]findCommonElements(可比[]集合){
if((集合[0]==null)| |(集合[1]==null)){
返回新的可比[0];
}
可比[]arr1=集合[0];
可比[]arr2=集合[1];
可比数组;
可比数组;
如果(arr1.length
顺便说一句,因为您有一个用于比较的字段。请确保正确处理该字段。每当启动新的findCommonElements或其他内容时,将其设置为0。您可以检查参数是否为null,如果其中任何一个为null,则可以返回空的Comparablearray(在空情况下这是正确的)
公共静态可比[]findCommonElements(可比[]集合){
if((集合[0]==null)| |(集合[1]==null)){
返回新的可比[0];
}
可比[]arr1=集合[0];
可比[]arr2=集合[1];
可比数组;
可比数组;
如果(arr1.length
顺便说一句,因为您有一个用于比较的字段。请确保处理正确。每当您启动新的findCommonElements或其他内容时,请将其设置为0。谢谢!这就是我缺少合并2D数组逻辑的地方!谢谢!这就是我缺少合并2D数组逻辑的地方!谢谢。我刚在运行时注意到了这一点在其他测试方法中进行比较。我的比较结果相差很远。如果正确的话,我会在secondTest()和thirdTest()方法的开头将变量比较值重置为0。firstTest()的预期输出是1个比较值,返回一个元素(“E”)。第二个应该是5,带“def G H”,第三个应该是2和“2 3”,在这种情况下,它们都显示了这一点。现在,将一些数据结构/算法纳入其中,比较与返回的公共元素相同,这是否意味着我实现了O(n)时间复杂度?谢谢。我刚刚注意到在运行其他测试方法时。我的比较有点不对劲。如果正确的话,我会在secondTest()和thirdTest()方法的开头将变量比较重置为0。firstTest()的预期输出是1个比较,返回一个元素(“E”)。第二个应该是5,带有“defgh”,第三个是2和“23”,在这种情况下,它们都会显示出来。现在,将一些数据结构/算法合并到其中,比较与返回的公共元素相同,这是否意味着我达到了O(n)的时间复杂度?