【问题标题】:Array of objects initialization?对象数组初始化?
【发布时间】:2018-06-22 18:47:07
【问题描述】:

我想创建这个答案中提到的对象数组:

Creating an array of objects in Java

即一次初始化一个

A[] a = new A[4];    
a[0] = new A();

我的班级定义:

public class Question {
    private int mAnswer;
    private String mQuestion;
    private String[] mOptions;

    public Question(String question, int answer, String[] option){
        mQuestion = question;
        mAnswer = answer;
        mOptions = option;
    }

    public int getmAnswer() {
        return mAnswer;
    }

    public String getmQuestion() {
        return mQuestion;
    }

    public String[] getmOptions() {
        return mOptions;
    }
};

我的 Java 活动:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import static android.R.attr.x;

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    }
    String[] options1 = {"ABCD", "EFGH", "IJKL", "MNOP"};
    String[] options2 = {"Bangalore", "Delhi", "Mumbai", "Pune"};
    String[] options3 = {"Business", "Work", "Nothing", "Study"};

    Question[] ques = new Question[3];

    ques[0] = new Question("What is your name?", 2, options1);

    ques[1] = new Question("Where are you from?", 3, options2);

    ques[2] = new Question("What do you do?", 4, options3);

}

但是初始化:

    ques[0] = new Question("What is your name?", 2, options1);

    ques[1] = new Question("Where are you from?", 3, options2);

    ques[2] = new Question("What do you do?", 4, options3);

给我一​​个错误。这些行带有红色下划线,为我提供了每一行的以下所有错误:

-需要标识符

-无效的方法声明;需要返回类型

-缺少方法体,或声明抽象

-预期参数

-意外的令牌

-未知类:“问题”

我使用时没有错误:

Question[] ques = new Question[]{new Question("What is your Name?", 2, options1), new Question("Where are you from?", 3, options2),new Question("What do you do?", 4, options3)};

为什么会这样?

我不知道我哪里出错了。我是 Android 和 Java 的初学者。 任何帮助,将不胜感激。 谢谢。

【问题讨论】:

  • 您有语法错误。在 setContentView 之后关闭函数。在 ques[2] = .. 之后放置右括号
  • 一般来说,如果你要将对象放入一个列表中,你应该使用 ArrayList 而不是你所展示的静态数组。

标签: java android arrays object initialization


【解决方案1】:

你不能在类的任何地方定义下面的行,而是应该移动到某个方法

ques[0] = new Question("What is your name?", 2, options1);
ques[1] = new Question("Where are you from?", 3, options2);
ques[2] = new Question("What do you do?", 4, options3);

你可能可以有一个方法名getQuestions 把它移进去

public Question[] getQuestions ()
{
    String[] options1 = {"ABCD", "EFGH", "IJKL", "MNOP"};
    String[] options2 = {"Bangalore", "Delhi", "Mumbai", "Pune"};
    String[] options3 = {"Business", "Work", "Nothing", "Study"};

    Question[] ques = new Question[3];

    ques[0] = new Question("What is your name?", 2, options1);

    ques[1] = new Question("Where are you from?", 3, options2);

    ques[2] = new Question("What do you do?", 4, options3);

    return ques;
}

【讨论】:

    【解决方案2】:

    因此,您使用的是静态数组而不是 ArrayList,您应该使用它来存储对象(字符串可以放入其中,但在 ArrayList 中更容易)。除了用于对象之外,主要区别在于 ArrayList 也是动态的(可以添加或删除对象)。

    执行以下操作:

    ArrayList<Question> list = new ArrayList<Question>();
    list.add(new Question("What is your name?", 2, options1));
    list.add(new Question("Where are you from?", 3, options2));
    list.add(new Question("What do you do?", 4, options3));
    

    要访问 arrayList 中的对象(例如第 0 个),只需执行以下操作:

    list.get(0);
    

    另一方面,这里的主要问题是您在类中为列表分配值,而不是在方法中。

    创建一个用于为列表分配值的方法,我相信它不会再崩溃了。类似于 Ravi 上面所做的:

    public Question[] getQuestions ()
    {
    // code for assigning the values that I wrote above.
    }
    

    【讨论】:

      【解决方案3】:

      您应该更喜欢 Collections 而不是 数组

      遗憾的是,您可以使用不同形式的数组初始化:

      Question[] ques = new Question[/*no size here*/] { // curly brace insteadof semicolon    
          ques[0] = new Question("What is your name?", 2, options1) , // comma instead of semicolon    
          ques[1] = new Question("Where are you from?", 3, options2),
          ques[2] = new Question("What do you do?", 4, options3) // no comma at last entry
      } // closing brace added
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-16
        • 2017-02-18
        • 2011-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多