【问题标题】:Passing array as a parameter of object problem将数组作为对象问题的参数传递
【发布时间】:2020-04-15 02:24:11
【问题描述】:

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

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 + '}';
    }
}

【问题讨论】:

    标签: java arrays object parameters


    【解决方案1】:

    teachers[1] 是数组中的第二个元素。数组本身就是teachers。喜欢,

    Publisher2 p1 = new Publisher2(teachers, 20);
    

    【讨论】:

    • 就是这样......但我希望 p1 对象包含第二个元素而不是孔数组。例如我想用 toString 方法写:Publisher2{teachers=Jyostna, y=20} 和 p2 :Publisher2{teachers=Sumathi, y=20}...
    • 那么它应该采用一个String,而不是String(s) 的数组。
    • 我在寻找更好的解决方案... :)
    • 教师是复数形式...您可能想澄清一位教师现在如何/为什么是复数形式。
    • 嗯...例如,我有一个数组(不必称为教师,它可能是 String 以及 int )并且我有带有两个参数的对象 o1,o2,o3。 o1 的参数之一必须是该数组的 index[1],其他对象的 o2 参数必须是 index[2],而 o3 的参数必须是 index[2]。所以,我找不到解决方案...我应该写一些方法还是什么?
    【解决方案2】:

    另一种可能性是使用可变参数并交换参数(可变参数必须是参数列表中的最后一个):

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

    使用这种方法,您可以传递尽可能多的参数(无、单个、多个或数组):

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

    对于toString 问题,您可以使用java.util.Arrays,这样toString 看起来像这样:

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

    然后字符串看起来像

    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)
    

    【讨论】:

    • 非常感谢。 Varargs 也是非常好的方法。两个答案都很棒。
    • 可变参数可能有助于语法便利,但无助于解决所提出的问题。
    • @AndrewF 为什么这对所提出的问题没有帮助?使用可变参数,手动创建数组的步骤被传递给编译器,因此new Publisher2(20, teachers[1]) 被编译为new Publisher2(20, new String[]{teachers[1]})
    • 问题陈述:“我想将数组作为对象的参数传递”——即作者要传递一个特定的集合/类型数据。使用 varargs 实现将允许使用相同调用语法的多个 arity(并且还可以将离散参数悄悄地转换为数组),这对于 other 用例可能很方便,但如果调用者知道他们想要什么要通过,则应专门键入该方法以允许该输入。
    【解决方案3】:

    由于您已经在各种 cmets 中澄清了您的问题(请编辑您的问题),我们现在知道您想要传递单个项目,但您在代码和 cmets 中暗示了 Publisher2必须以数组为参数构造。

    因此,您需要构建一个包含单个项目的新数组。

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

    或者...

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

    请编辑您的问题,以便清楚您想要完成的确切目标。

    【讨论】:

      猜你喜欢
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 2016-08-28
      相关资源
      最近更新 更多