java - 将数组作为对象的参数传递问题

标签 java arrays object parameters

我想将数组作为对象的参数传递,但我是一种堆栈。 这就是我的问题。

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/

相关文章:

java - Hibernate 重复数据库条目

javascript - 将对象属性和值转换为字符串

Java - 在没有方法的情况下编辑实例变量

Java 按顺序迭代并打印多个数组

java - Java 环境中的 Silverlight 模拟

java - 如何禁用和启用 USB OTG 连接以打开和关闭硬件设备?

Java:排序索引数组

c++ - 在C++中为char数组分配固定内存

java - 在 n 个数字的数组/列表中查找 3 个最大的数字而不进行排序

java - java中的socket问题