对象数组初始化?

时间:2018-01-13 14:45:50

标签: java android arrays object initialization

我想创建一个对象数组,如本答案中所述:

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);

给我一​​个错误。这些行加下划线RED给出了每行的以下所有错误:

  

-Identifier expected

     

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

     

-Missing method body,或声明abstract

     

- 参数预期

     

- 意外的令牌

     

-Unknown class:'ques'

我使用时没有错误:

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的初学者。 任何帮助,将不胜感激。 感谢。

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

执行以下操作:

SQLite error (26): file is not a database
Exception thrown: 'System.Data.SQLite.SQLiteException' in System.Data.SQLite.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>dbtool.exe</AppDomain><Exception><ExceptionType>System.Data.SQLite.SQLiteException, System.Data.SQLite, Version=1.0.106.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139</ExceptionType><Message>file is not a database
file is not a database</Message><StackTrace>   at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String&amp;amp; strRemain)
   at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
   at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
   at System.Data.SQLite.SQLiteDataReader.NextResult()
   at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
   at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SQLite.SQLiteCommand.ExecuteReader()

要访问arrayList中的对象(例如0),只需执行:

DB Browser for SQLite

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

制作一个方法用于为列表分配值,我不相信它会再崩溃。像Ravi上面所说的那样:

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));

答案 2 :(得分:-2)

您应该更喜欢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
相关问题