我想将数组作为对象的参数传递,但是我有点堆栈。
所以这是我的问题。
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 + '}';
}
}
最佳答案
teachers[1]
是数组中的第二个元素。数组本身只是teachers
。喜欢,
Publisher2 p1 = new Publisher2(teachers, 20);