4

There's a String array that I have which has setter and getter methods. How do I directly pass values {"one", "two"} to the setter method rather than setting the values to a variable first and then passing the parameter?

String[] arr1 = {};
public String[] getArr1() {
    return arr1;
}

public void setArr1(String[] arr1) {
    this.arr1 = arr1;
}

..expecting something like setArr1(?);...

1
  • 2
    Thanks all!! setArr1(new String[]{"One", "Two"}); is working fine!! Commented Jan 1, 2013 at 6:02

6 Answers 6

6

You could use setArr1(new String[]{"one", "two"})

Alternatively, you could make use of varargs and change your method signature to setArr1(String... values) and use the method as setArr1("one", "two")

Sign up to request clarification or add additional context in comments.

Comments

2
setArr1(new String[]{"one","two","three"});

Try like this

Comments

2
someobject.setArr1(new String[]{"One", "Two"});

Please refer my answer on arrays for more info on array intialization.

Comments

1

try this - setArr1(new String[]{"one", "two"})

Comments

0

use this code this is setter method

public void setSubTaught(int pos, String value)
{
    subjectsTaught[pos]=value;
}

this is getter method

public String getSubTaught()
{
    String sub = Arrays.toString(subjectsTaught);
    int len = sub.length();
    String fix = sub.substring(1,len-1);
    return fix;
}

Comments

0

Your setArr1(String[] arr1) is taking array of string as a parameter so you can set as normal way we set setter methods.

yourObject.setArr1(new String[]{"string1", "string2"});

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.