Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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_Arraylist - Fatal编程技术网

Java 添加到指定索引时arraylist的行为

Java 添加到指定索引时arraylist的行为,java,arraylist,Java,Arraylist,我知道可以将对象添加到指定索引处的arraylist中,但我想知道放置在这些索引处的对象之前、之后和之间的索引会发生什么变化 我正在实施一个考试评分系统,我有一个Student getAnswers()方法,可以检索学生在考试中提供的所有答案。我将其存储在arrayList中 ArrayList<Answer> studentAnswers = new ArrayList<Answer>(); 我的问题是,如果学生没有回答其中一个问题,我希望它是一个空答案。我不知道这是

我知道可以将对象添加到指定索引处的arraylist中,但我想知道放置在这些索引处的对象之前、之后和之间的索引会发生什么变化

我正在实施一个考试评分系统,我有一个Student getAnswers()方法,可以检索学生在考试中提供的所有答案。我将其存储在arrayList中

ArrayList<Answer> studentAnswers = new ArrayList<Answer>();
我的问题是,如果学生没有回答其中一个问题,我希望它是一个空答案。我不知道这是不是正确的处理方法。 例如:

StudentAnswer [question 1, "Foo"]
StudentAnswer [question 2, "Class"]
StudentAnswer [question 4, "Object"]
如果答案键是
[“Foo”,“Class”,“Interface”,“Object”]

学生回答列表应如下所示:
[“Foo”,“Class”,Null,“Object”]

空,因为他没有回答问题3


我不知道这是否是处理这个问题的正确方法。

我觉得数组而不是ArrayList更适合解决这个问题,因为您需要使用大量的问题来初始化数组的大小

import java.util.Arrays;

class StudentAnswer {
    private int id;
    private String value;

    public StudentAnswer(int id, String ans) {
        this.id = id;
        this.value = ans;
    }

    public int getQuestionNumber() {
        return this.id;
    }

    public String toString() {
        return this.value;
    }
}

class Main {
  public static void main(String[] args) {
    StudentAnswer a0 = new StudentAnswer(0, "Foo");
    StudentAnswer a1 = new StudentAnswer(1, "Class");
    StudentAnswer a3 = new StudentAnswer(3, "Object");

    int totalQuestions = 4;
    StudentAnswer[] answers = new StudentAnswer[totalQuestions];
    answers[a0.getQuestionNumber()] = a0;
    answers[a1.getQuestionNumber()] = a1;
    answers[a3.getQuestionNumber()] = a3;

    System.out.println(Arrays.asList(answers));
  }
}
输出
注意:除非您在
StudentAnswer
中修改
toString
方法,否则引号不会显示在字符串上

我觉得数组而不是ArrayList更适合这个问题,因为您需要用您的问题数量初始化数组的大小

import java.util.Arrays;

class StudentAnswer {
    private int id;
    private String value;

    public StudentAnswer(int id, String ans) {
        this.id = id;
        this.value = ans;
    }

    public int getQuestionNumber() {
        return this.id;
    }

    public String toString() {
        return this.value;
    }
}

class Main {
  public static void main(String[] args) {
    StudentAnswer a0 = new StudentAnswer(0, "Foo");
    StudentAnswer a1 = new StudentAnswer(1, "Class");
    StudentAnswer a3 = new StudentAnswer(3, "Object");

    int totalQuestions = 4;
    StudentAnswer[] answers = new StudentAnswer[totalQuestions];
    answers[a0.getQuestionNumber()] = a0;
    answers[a1.getQuestionNumber()] = a1;
    answers[a3.getQuestionNumber()] = a3;

    System.out.println(Arrays.asList(answers));
  }
}
输出
注意:除非您在
StudentAnswer
中修改
toString
方法,否则引号不会显示在字符串上

如果学生没有回答,考虑让getAnswers()方法返回null如果学生没有回答,考虑让getAnswers()方法返回null
[Foo, Class, null, Object]