Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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 类,类似于Swift中的结构_Java_Android - Fatal编程技术网

Java 类,类似于Swift中的结构

Java 类,类似于Swift中的结构,java,android,Java,Android,在我的swift应用程序中,我有以下代码: struct Question { var QuestionLbl : String! var Answers : [String]! var Answer : Int! } 然后我像这样使用这个结构 var Questions = [Question]() Questions = [Question(QuestionLbl: "Whats my name?", Answers: ["John","J

在我的swift应用程序中,我有以下代码:

struct Question
{
    var QuestionLbl : String!
    var Answers : [String]!
    var Answer : Int!
}
然后我像这样使用这个结构

var Questions = [Question]()

Questions =
            [Question(QuestionLbl: "Whats my name?", Answers: ["John","Josh","Adam","Leo"], Answer: 0),

                Question(QuestionLbl: "Whats my moms name?", Answers: ["Jessica","Crystal","Samanta","Kate"], Answer: 3),

                Question(QuestionLbl: "Whats my fathers name?", Answers: ["Ed","Blake","Jeff","Jonhson"], Answer: 2)]
现在,我正在尝试为Android制作相同的应用程序。。 所以我创建了一个类,并尝试做同样的事情。。 这是类文件:

public class Question {
    String questionLabel;
    String[] answersOptions;
    Integer correctAnswer;

    public Question(String questionLabel, String[] answersOptions, Integer correctAnswer) {
        this.questionLabel = questionLabel;
        this.answersOptions = answersOptions;
        this.correctAnswer = correctAnswer;
    }

    public String getquestionLabel() {
        return questionLabel;
    }

    public String[] getanswersOptions() {
        return answersOptions;
    }

    public Integer getcorrectAnswer() {
        return correctAnswer;
    }
}
这就是我在主要活动中尝试的方式:

Question[] questions;
        questions = {
                 Question("Whats my name?",{"John","Josh","Adam","Leo"}, 1),
                 Question("Whats my mom's name?",{"Jessica","Crystal","Samanta","Kate"}, 1)
        };
但它不起作用。 问题出在哪里?

请查看post,了解声明数组的语法。基本上,它看起来像:

questions = new Question[]{
    new Question("What's my name?", new String[]{"John","Josh","Adam","Leo"}, 1),
    new Question("What's my mom's name?", new String[]{"Jessica","Crystal","Samanta","Kate"}, 1)
    //etc..
}

您需要使用
new
关键字。您必须使用new关键字来执行此操作。