当您必须为数据层次结构创建一个二维数组时,您可能应该为它创建一个合理的模型。
这是适合您的快速(且肮脏)模型(因打字速度而丢弃的设置器):
问卷类:
/**
* Facilitates an entire questionnaire
*/
public class Questionnaire extends ArrayList<Question> {
/**
* This questionnaire's name
*/
private String name;
/**
* Creates a new questionnaire using the specified name
* @param name The name of this questionnaire
*/
public Questionnaire(String name) {
this.name = name;
}
/**
* Returns the name of this questionnaire
*/
public String getName() {
return name;
}
}
问题类:
/**
* Facilitates a question and its answers
*/
public class Question extends ArrayList<Answer> {
/**
* The question's text
*/
private String text;
/**
* Constructs a new question using the specified text
* @param text The question's text
*/
public Question(String text) {
this.text = test;
}
/**
* Returns this question's text
*/
public String getText() {
return text;
}
}
答题类:
/**
* Facilitates an answer
*/
public class Answer {
/**
* The answer's text
*/
private String text;
/**
* Whether or not this answer is correct
*/
private boolean correct;
/**
* Constructs a new answer using the specified settings
* @param text The text of this answer
* @param correct Whether or not this answer is correct
*/
public Answer(String text, boolean correct) {
this.text = text;
this.correct = correct;
}
/**
* Returns this answer's text
*/
public String getText() {
return text;
}
/**
* Whether or not this answer is correct
*/
public boolean isCorrect() {
return correct;
}
}
它的用法如下:
// Create a new questionnaire
Questionnaire questionnaire = new Questionnaire("The awesome questionnaire");
// Create a question and add answers to it
Question question = new Question("How awesome is this questionnaire?");
question.add(new Answer("It's pretty awesome", false));
question.add(new Answer("It's really awesome", false));
question.add(new Answer("It's so awesome my mind blows off!", true));
// Add the question to the questionnaire
questionnaire.add(question);
迭代它非常容易:
// Iterate over the questions within the questionnaire
for(Question question : questionnaire) {
// Print the question's text
System.out.println(question.getText());
// Go over each answer in this question
for(Answer answer : question) {
// Print the question's text
System.out.println(answer.getText());
}
}
您也可以只迭代其中的一部分:
// Iterate over the third to fifth question
for (int questionIndex = 2; questionIndex < 5; questionIndex ++) {
// Get the current question
Question question = questionnaire.get(questionIndex);
// Iterate over all of the answers
for (int answerIndex = 0; answerIndex < question.size(); answerIndex++) {
// Get the current answer
Answer answer = question.get(answerIndex);
}
}
使用您描述的格式将文件读入模型可以通过以下方式完成:
// Create a new questionnaire
Questionnaire questionnaire = new Questionnaire("My awesome questionnaire");
// Lets scan the file and load it up to the questionnaire
Scanner scanner = new Scanner(new File("myfile.txt"));
// Read lines from that file and split it into tokens
String line, tokens[];
int tokenIndex;
while (scanner.hasNextLine() && (line = scanner.nextLine()) != null) {
tokens = line.split(",");
// Create the question taking the first token as its text
Question question = new Question(tokens[0]);
// Go over the tokens, from the first index to the one before last
for (tokenIndex = 1; tokenIndex < tokens.length-1; tokenIndex++) {
// Add the incorrect answer to the question
question.add(new Answer(tokens[tokenIndex], false));
}
// Add the last question (correct one)
question.add(new Answer(tokens[tokenIndex],true));
}
// Tada! questionnaire is now a complete questionnaire.