使用String []作为参数之一创建对象的实例

时间:2018-04-07 15:17:58

标签: java arrays

我想知道如何创建一个以String []类型作为参数的对象实例。到目前为止,它出现了一个错误,说我在创建新实例时无法初始化数组。这是我的代码:

public class Profile{

    public Profile(String[] interests) {

        this.interests = interests;

    }
}

public class ProfileTest{

    public static void main(String[] args) {

        //Where the error appears
        Profile newProfile = new Profile({"Interest1","Interest2","Interest3"});

    }
}

我不想使用arraylist或任何东西 - 只是字符串[]。

1 个答案:

答案 0 :(得分:0)

要修复编译错误,请使用变量来保存字符串数组,如下所示:

String[] array = {"Interest1","Interest2","Interest3"};

Profile newProfile = new Profile(array);