3

Having trouble creating a constructor that takes multiple one dimensional arrays of Strings:

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), misplaced construct(s) - Type mismatch: cannot convert from String[] to relation - Syntax error on token "}", delete this token - Syntax error on token ")", } expected

I need to be able to use each array separately withing the relation class.

1
  • 1
    It's off-topic here, but normally, in java, we write class names in CamelCase Commented Jun 4, 2015 at 7:59

3 Answers 3

5

You can't use array literals that way in Java - you have to explicitly initialize them. e.g.:

relation relation1 = new relation(new String[]{"1","2","3","4","5"}, 
                                  new String[]{"1","2","3","4"},
                                  new String[]{"2","3","4","5"});
Sign up to request clarification or add additional context in comments.

3 Comments

Note that String []arr = {"1","2"}; is still allowed.
@ChetanKinger yup, but that's the only place it'll fly
@Mureinik True. The compiler replaces it with String []arr = new String[]{"1","2"}; I wonder why the limitation was imposed on using the same shorthand for arguments. Also, I prefer using varargs over an array as a parameter in such situations.
1

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

2 Comments

Nicely formatted. Other than that, does not add much value over the accepted answer. Especially when it is posted 7 minutes later..
Yes - agree nicely fomatted and follow Java naming conventions.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.