具有不同数组参数的构造函数 java

constructors with different array-arguments java

我正在尝试使用数组参数创建不同的构造函数。数组具有不同的维度。但实际上二维构造函数随时调用。

public Network(String [] adjacentMatrixRows) throws AdjacentMatrixCreationExeption {
    this.adjacentMatrix = new int[adjacentMatrixRows.length][];
    String [][] stringMatrix = new String[adjacentMatrixRows.length][];
    for (int i = 0; i < adjacentMatrixRows.length; i++) {
        stringMatrix[i] = adjacentMatrixRows[i].trim().split(" ");
        this.adjacentMatrix[i] = new int[stringMatrix[i].length];
    }
    for (int i = 0; i < stringMatrix.length; i++){
        for (int j = 0; j < stringMatrix[i].length; j++) {
            try {
                this.adjacentMatrix[i][j] = Integer.parseInt(stringMatrix[i][j]);
            }
            catch (NumberFormatException e){
                throw  new AdjacentMatrixCreationExeption("Wrong data format in String[].");
            }
        }
    }
}
public Network(String[][] adjacentMatrix) throws AdjacentMatrixCreationExeption {
    this.adjacentMatrix = new int [adjacentMatrix.length][];
    for (int i = 0; i < adjacentMatrix.length; i++){
        this.adjacentMatrix[i] = new int[adjacentMatrix[i].length];
        for (int j = 0; j < adjacentMatrix[i].length; j++) {
            try {
                this.adjacentMatrix[i][j] = Integer.parseInt(adjacentMatrix[i][j]);
            }
            catch (NumberFormatException e){
                throw  new AdjacentMatrixCreationExeption("Wrong data format in String[][].");
            }
        }
    }
}

可能是问题所在,因为数组由数组组成。但是我该如何解决呢?

更新: 我正在尝试在 Junit 测试中调用构造函数:

` Throwable exception = assertThrows(AdjacentMatrixCreationExeption.class, ()-> {
             Network network = new Network(new String [][]{{"1", "a"},{"1", "1"}});});
        assertEquals("Wrong data format in String[][].", exception.getMessage());`

     Throwable exception2 = assertThrows(AdjacentMatrixCreationExeption.class, ()-> {
                Network network = new Network(new String []{"1", "a", "1", "1"});});
            assertEquals("Wrong data format in String[].", exception.getMessage());

JUnit 测试的输出:

org.opentest4j.AssertionFailedError: expected: <Wrong data format in String[].> but was: <Wrong data format in String[][].>
Expected :Wrong data format in String[].
Actual   :Wrong data format in String[][].

我做了下面的测试

public class Test {

    public static void main(String[] args) {
        String[] array = new String[1];
        String[][] array2D = new String[1][1];

        Network n1 = new Network(array);
        Network n2 = new Network(array2D);
    }

}

这是具有两个构造函数的网络 class:

class Network {
    public Network (String [] adjacentMatrixRows){
        System.out.println("Array");
    }

    public Network (String [][] adjacentMatrixRows){
        System.out.println("2D Array");
    }
}

我得到以下输出:

Array

2D Array

我找到答案了!在我的 JUnit 测试中,我总是使用名为 "exception" 的单一异常。但是在第二次测试中,我不得不使用 "exception2"

Throwable exception2 = assertThrows(AdjacentMatrixCreationExeption.class, ()-> {
        Network network = new Network(new String []{"1", "a", "1", "1"});});
    assertEquals("Wrong data format in String[].", exception2.getMessage());

谢谢大家。问题已解决。