如何在类构造函数中初始化数组

时间:2018-04-16 05:30:48

标签: java arrays class constructor initialization

我到处寻找,但我找不到与在类构造函数中初始化数组相关的任何内容。

这是我尝试创建的课程:

 public Sandwich(char b, String m, String c, String[] e)
 {
    setBread( b );
    setMeat( m );
    setCheese( c );
    setExtras( e );
 }

这是我尝试初始化数组:

public static void main(String[] args)
{
    Sandwich[] order;
    order = new Sandwich[3];

    order[0] = new Sandwich('s', "pastrami", "swiss", extras[2] = {"mayo", "extra meat"}  )
    order[1] = new Sandwich();
}

我不知所措。

2 个答案:

答案 0 :(得分:1)

选项1:使用 SELECT mobile_no FROM A WHERE NOT EXISTS(SELECT mobile_no FROM B where A.mobile_no = B.mobile_no). 制作和数组:

new

选项2:使用VarArgs:

将构造函数重新定义为:

new Sandwich('s', "pastrami", "swiss", new String[]{"mayo", "extra meat"});

将值传递给构造函数,从第4个参数开始,所有都是数组元素:

public Sandwich(char b, String m, String c, String... e)

答案 1 :(得分:0)

你可以试试这个:

bot.use({
    recieve: function(event, next){
        locale = event.textLocale; // you can use textLocale
        console.log("event runs");
        eventTimestamp = event.timestamp; // this event also provides timestamp
        next(); // don't forget to call next
    }
});

参考java教程About array