Using multiple array constructors in Java

There was a problem creating a constructor with multiple one-dimensional string arrays:

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","4"},{"2","5"});
    relation relation2 = new relation({"a","b","c","d"},{"a","d","c"},"b"});

}

}

I continue to receive multiple errors - syntax error on token, misplaced construct (s) - type mismatch: unable to convert from string [] to relationship - syntax error on token "}", delete this token - syntax error on token ")",}}

I need to be able to use relational classes to use each array separately

Solution

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

relation relation1 = new relation(new String[]{"1",new String[]{"1",new String[]{"2","5"});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>