如何初始化数组以便稍后在测试方法中使用 (Java)

How to initialize an array to use in a test method later (Java)

我正在比较两个数组中的元素并打印这些元素以及比较次数。

在 "findCommonElements" 方法中,我不能有空数组,否则会出现 NullPointerException(当然)错误。

我不能只声明它们,否则会出现 "variable may have not been initialized" 错误。

我必须使用这些数组的多个版本进行测试,但我不确定该怎么做。该程序仅读取数组的第一个实例。

代码:

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 方法中的数组,而不是 firstTest() 方法(其他测试方法相同,除了数组中的值,所以它们被注释掉了) :

    First Test
    Comparisons: 2
    Common Elements: C D

注意 是的,我在 findCommonElements 方法中有一个未使用的参数。这是一个要求,我不明白如何将二维数组合并到这个......至少现在还没有。

是的,数组是 Comparable 类型的。这是另一个要求。

另外,如果我的逻辑在这段代码中有任何不正确的地方,我深表歉意。我只是想暂时解决这个问题。

要声明数组,您应该使用以下语法:

// Type[] array = new Type[arrayLenght];

例如,如果你想创建一个长度为 10Comparable 数组,你应该这样做:

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 = collections[0];
  Comparable[] arr2 = collections[1];

然后您的测试开始工作。

您可以检查您的参数是否为空,如果其中任何一个为空,您可以 return 一个空的 Comparablearray(在空的情况下是正确的)。

  public static Comparable[] findCommonElements(Comparable[][] collections) {
    if((collections[0]==null) || (collections[1]==null)){
    return new Comparable[0];
    }
    Comparable[] arr1 = collections[0];
    Comparable[] arr2 = collections[1];
    Comparable[] hashArray;
    Comparable[] searchArray;
    if(arr1.length < arr2.length) {
    [...]

顺便说一句。因为你有一个比较领域。确保你处理得当。每当您启动新的 findCommonElements 或其他东西时,将其设置为 0。