我想将数组作为对象的参数传递,但我是一种堆栈。 这就是我的问题。
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);
关于java - 将数组作为对象的参数传递问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59461381/