Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调用一个超类';具有包含数据的新数组或ArrayList的构造函数?_Java_Arrays_Inheritance_Constructor_Arraylist - Fatal编程技术网

Java 调用一个超类';具有包含数据的新数组或ArrayList的构造函数?

Java 调用一个超类';具有包含数据的新数组或ArrayList的构造函数?,java,arrays,inheritance,constructor,arraylist,Java,Arrays,Inheritance,Constructor,Arraylist,我试图调用一个超类的构造函数,使用ArrayList(首选)或已经包含信息的数组。我不知道我的语法错误是什么,或者你是否能做到 我特别想在任何一个对象中插入“true”和“false”。就那两个 public class TrueFalseQuestion extends MultipleChoiceQuestion { Answer myAnswer; StringPrompt myPrompt; //I can create, but not initialize

我试图调用一个超类的构造函数,使用ArrayList(首选)或已经包含信息的数组。我不知道我的语法错误是什么,或者你是否能做到

我特别想在任何一个对象中插入“true”和“false”。就那两个

public class TrueFalseQuestion extends MultipleChoiceQuestion
{
    Answer myAnswer;
    StringPrompt myPrompt;

    //I can create, but not initialize data up here, correct?
    //Tried creating an ArrayList, but cannot insert the strings before calling constructor

    public TrueFalseQuestion()
    {
        /* Want to call parent constructor with "true" and "false" in array or ArrayList already*/
        super(new String["true", "false"]);
        ...
    }
我有一种感觉,这是一个面包师,但我就是想不出来。我尝试过各种方法,但痛苦的是必须首先调用超级构造函数,因此没有给我初始化数据的机会。

使用以下格式:

super(new String[]{"true", "false"});
super(new String[] {"true", "false"});
如果
MultipleChoiceQuestion
包含如下构造函数:

MultipleChoiceQuestion(String[] questionArray)
public TrueFalseQuestion() {
    super("true", "false");
}
如果如您所说,它包含一个带有
列表
参数的重载构造函数,例如:

MultipleChoiceQuestion(List<String> questionList)
或者如果需要使用
ArrayList

super(new ArrayList<String>(Arrays.asList(new String[] { "true", "false" })));
super(新的ArrayList(Arrays.asList)(新字符串[]{“true”,“false”}));
对于
列表
(不是专门的
数组列表
),可以执行以下操作:

super(Arrays.asList("true", "false"));
如果您特别需要
阵列列表
,则需要执行以下操作:

super(new ArrayList<String>(Arrays.asList(new String[]{"true", "false"})));
super(新的ArrayList(Arrays.asList)(新字符串[]{“true”,“false”}));

如果您对MultipleChiiceQuestion类有任何控制权,您可以将构造函数改为:

public MultipleChoiceQuestion(String ... values) {}
你可以这样称呼它:

MultipleChoiceQuestion(String[] questionArray)
public TrueFalseQuestion() {
    super("true", "false");
}

如果你以前没有听说过,这叫做varargs。您可以在这里阅读:

谢谢!有没有办法用ArrayList也能做到这一点?@iaacp yes,
super(Arrays.asList(“true”、“false”)
可以按照Reimeus的建议使用。谢谢:)你知道ArrayList是否也可以使用吗?只是添加了它。希望这有帮助。
Arrays.asList
返回一个
List
,而不是
ArrayList
。(在当前的实现中,返回的实际对象不是
ArrayList
的实例;它是私有类
Arrays.ArrayList
的实例),这很有帮助。非常感谢。我无法决定使用哪个实现。那可能是最好的。它给你很大的自由。您仍然可以向构造函数发送数组。两种方法都会奏效。