0

I want to pass array as a parameter of object but I am kind of stack. So here is my problem.

public class Publisher {

    public static void main(String args[]) {
        String teachers[] = {"Jyothi", "Jyostna", "Sumathi"};
        Publisher2 p1 = new Publisher2(teachers[1],20);       //The problem is here, I can't specify 
     //index of array (I can put onlu teachers without index[1])so toString method is returning address of object instead of the name
        System.out.println(p1);
    }
}

class Publisher2 {

    String[] teachers;
    int y;

    public Publisher2(String[] teachers, int y) {
        this.teachers = teachers;
        this.y = y;
    }

    @Override
    public String toString() {
        return "Publisher2{" + "teachers=" + teachers + ", y=" + y + '}';
    }
}
0

3 Answers 3

2

teachers[1] is the second element in your array. The array itself is just teachers. Like,

Publisher2 p1 = new Publisher2(teachers, 20);
Sign up to request clarification or add additional context in comments.

6 Comments

That's it...But I want p1 object to consist that second element and not the hole array. For example I want toString method to write : Publisher2{teachers=Jyostna, y=20} and for p2 :Publisher2{teachers=Sumathi, y=20}...
Then it should take one String, not an array of String(s).
I was hopping for some better solution... :)
Teachers is plural... you might want to clarify how/why one teacher is now multiple.
Well... For example i have one array (doesn't have to be called teacher and it may be String as well as int ) and I have objects o1,o2,o3 with two parameters. One of that parameters for o1 has to be index[1] of that array, for other object o2 parameter has to be index[2], and for o3 parameter has to be index[2]. So, I can not find solution for that...Should I write some method or something?
|
0

Another possibility would be to use varargs and swap the parameters (the varargs have to be the last in the parameter list):

public Publisher2(int y, String... teachers) {
  this.teachers = teachers;                   
  this.y = y;                                 
}                                             

With this approach you can pass as many arguments (none, a single, multiple or an array):

new Publisher2(20);
new Publisher2(20, teachers[0]);
new Publisher2(20, teachers);

With the toString problem you can use java.util.Arrays, so that the toString looks like this:

@Override                                                                           
public String toString() {                                                          
  return "Publisher2{" + "teachers=" + Arrays.toString(teachers) + ", y=" + y + "}";
}                                                                                   

The strings then will look like

Publisher2{teachers=[], y=20} // for new Publisher(20)
Publisher2{teachers=[Jyostna], y=20} // for new Publisher(20, teachers[1])
Publisher2{teachers=[Jyothi, Jyostna, Sumathi], y=20}
 // for new Publisher(20, teachers)

4 Comments

Thank you very much. Varargs are also very good approach. Both answers are great.
varargs might help for syntactic convenience but does not help with the problem as presented.
@AndrewF Why doesn't this help to the problem as presented? With the varargs the step to manually create an array gets passed to the compiler, so new Publisher2(20, teachers[1]) gets compiled to new Publisher2(20, new String[]{teachers[1]})
The problem statement: "I want to pass array as a parameter of object" -- that is, there is one specific set/type data that the author wants to pass. Implementing with varargs would allow for multiple arity with the same call syntax (and also for quietly converting discrete parameters into an array), which might be convenient for other use cases, but if the caller knows what they want to pass, then the method should be typed specifically to allow that one input.
0

Since you have clarified your question in various comments (please edit your question), we now know that you want to pass a single item, but you have implied in code and comments that Publisher2 must be constructed with an array as a parameter.

Therefore, you will need to build a new array containing the single item.

String[] teacherSubset = new String[] { teachers[1] };
Publisher2 p1 = new Publisher2(teacherSubset, 20);

or...

Publisher2 p1 = new Publisher2(new String[] { teachers[1] }, 20);

Please edit your question so that it is clear about exactly what you are trying to accomplish.

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.