java 中使用多个数组的构造函数

Constructor using multiple arrays in java

在创建采用多个 String 的一维数组的构造函数时遇到问题:

class relation {

String[] setA, setB, setC;

relation (String[] setA, String[] setB, String[] setC) {
    this.setA = setA;
    this.setB = setB;
    this.setC = setC;
} 
}

public class matrix {

public static void main(String[] args) {

    relation relation1 = new relation({"1","2","3","4","5"}, {"1","2","3","4"}, {"2","3","4","5"});
    relation relation2 = new relation({"a","b","c","d"}, {"a","b","c","d","b","c"}, {"a","b","c","d","c","b"});

}

}

我不断收到多个错误 - 标记语法错误,错误放置的结构 - 类型不匹配:无法从 String[] 转换为 关系 - 标记“}”的语法错误,删除该标记 - 标记“)”的语法错误,} expected

我需要能够根据关系 class.

单独使用每个数组

您不能在 Java 中以这种方式使用数组文字 - 您必须显式初始化它们。例如:

relation relation1 = new relation(new String[]{"1","2","3","4","5"}, 
                                  new String[]{"1","2","3","4"},
                                  new String[]{"2","3","4","5"});

你可以这样做 -

class Relation {
    String[] setA, setB, setC;
    Relation(String[] setA, String[] setB, String[] setC) {
        this.setA = setA;
        this.setB = setB;
        this.setC = setC;
    }
}

public class Assignment3 {

    public static void main(String[] args) {
        Relation relation1 = new Relation(
                new String[]{"1", "2", "3", "4", "5"}, new String[]{"1", "2",
                        "3", "4"}, new String[]{"2", "3", "4", "5"});
        Relation relation2 = new Relation(new String[]{"a", "b", "c", "d"},
                new String[]{"a", "b", "c", "d", "b", "c"}, new String[]{"a",
                        "b", "c", "d", "c", "b"});
    }
}

像这样尝试它会起作用

relation relation1 = new relation(new String[]{"1","2","3","4","5"},
                        new String[]{"1","2","3","4"},new String[]{"2","3","4","5"});