Constructor using multiple arrays in java

Can't create constructor that accepts multiple one-dimensional arrays String

s:

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"});

}

}

      

I keep getting multiple errors - Syntax error on token (s), incorrect construct (s) - Type mismatch: cannot be converted from String [] to link - Syntax error on token "}", remove this token - Syntax error on token ")",} expected

I need to be able to use each array separately with a relationship class.

+3


source to share


3 answers


You cannot use array literals this way in Java - you must initialize them explicitly. eg:.



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

      

+5




You can do it like this:



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"});
    }
}

      

+1


source


Try this it will work

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

      

+1


source







All Articles