Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 数组列表故障_Java_Arrays_Arraylist - Fatal编程技术网

Java 数组列表故障

Java 数组列表故障,java,arrays,arraylist,Java,Arrays,Arraylist,我正在做一个项目,并没有使用数组,我发现数组列表会更好。 我知道我需要声明数组列表及其方法,但我不太确定从那里开始。有什么建议吗? 这是代码 public class TestScoresModel { private ArrayList<Integer> tests; // private Student[] students; // Array of students private int indexSelectedStudent;

我正在做一个项目,并没有使用数组,我发现数组列表会更好。
我知道我需要声明数组列表及其方法,但我不太确定从那里开始。有什么建议吗?
这是代码

  public class TestScoresModel {
    private ArrayList<Integer> tests;
    // private Student[] students;         // Array of students
    private int indexSelectedStudent;      // Position of current student
    private int studentCount;              // Current number of students

    public TestScoresModel() {

      // Initialize the data
      indexSelectedStudent = -1;
      studentCount = 0;
      // students = new Student[10];
      ArrayList list = new ArrayList();
    }

    // Mutator methods for adding and replacing students
    public String add(Student s) {
      if (studentCount == .length)
        return "SORRY: student list is full";
      else {
        students[studentCount] = s;
        indexSelectedStudent = studentCount;
        studentCount++;
        return null;
      }
    }

    public String replace(Student s){
      if (indexSelectedStudent == -1)
       return "Must add a student first";
      else {     
        students[indexSelectedStudent] = s;
        return null;
      }
    }

    // Navigation methods

    public Student first() {
      Student s = null;
      if (studentCount == 0)
        indexSelectedStudent = -1;
      else {
        indexSelectedStudent = 0;
        s = students[indexSelectedStudent];
      }
      return s;
    }

    public Student previous() {
      Student s = null;
      if (studentCount == 0)
       indexSelectedStudent = -1;
      else {
        indexSelectedStudent = Math.max (0, indexSelectedStudent - 1);
        s = students[indexSelectedStudent];
      }
      return s;
    }

    public Student next() {
      Student s = null;
      if (studentCount == 0)
        indexSelectedStudent = -1;
      else {
        indexSelectedStudent = Math.min (studentCount - 1, indexSelectedStudent + 1);
        s = students[indexSelectedStudent];
      }
      return s;
    }

    public Student last(){
      Student s = null;
      if (studentCount == 0)
        indexSelectedStudent = -1;
      else {
        indexSelectedStudent = studentCount - 1;
        s = students[indexSelectedStudent];
      }
      return s;
    }

    // Accessors to observe data

    public Student currentStudent() {
      if (indexSelectedStudent == -1)
        return null;
      else
        return students[indexSelectedStudent];
    }

    public int size() {
      return studentCount;
    }

    public int currentPosition() {
      return indexSelectedStudent;
    }

    public int getClassAverage(){
      if (studentCount == 0)
        return 0;
      int sum = 0;
      for (int i = 0; i < studentCount; i++)
        sum += students[i].getAverage();
      return sum / studentCount;
    }

    public Student getHighScore() {
      if (studentCount == 0)
        return null;
      else {
        Student s = students[0];
        for (int i = 1; i < studentCount; i++)
          if (s.getHighScore() < students[i].getHighScore())
            s = students[i];
        return s;
      }
    }

    public String toString() {
      String result = "";
        for (int i = 0; i < studentCount; i++)
          result = result + students[i] + "\n";
        return result;
    }
}
公共类TestScoresModel{
私有数组列表测试;
//私人学生[]学生;//学生数组
private int indexSelectedStudent;//当前学生的位置
private int studentCount;//当前学生人数
公共TestScoresModel(){
//初始化数据
indexSelectedStudent=-1;
学生人数=0;
//学生=新生[10];
ArrayList=新建ArrayList();
}
//添加和替换学生的Mutator方法
公共字符串添加(学生){
if(studentCount=.length)
返回“抱歉:学生名单已满”;
否则{
学生[学生人数]=s;
indexSelectedStudent=学生人数;
studentCount++;
返回null;
}
}
公共字符串替换(学生s){
如果(indexSelectedStudent==-1)
return“必须先添加学生”;
否则{
学生[indexSelectedStudent]=s;
返回null;
}
}
//导航方法
公立学生优先(){
学生s=null;
if(studentCount==0)
indexSelectedStudent=-1;
否则{
indexSelectedStudent=0;
s=学生[索引选定学生];
}
返回s;
}
公立学生(前){
学生s=null;
if(studentCount==0)
indexSelectedStudent=-1;
否则{
indexSelectedStudent=Math.max(0,indexSelectedStudent-1);
s=学生[索引选定学生];
}
返回s;
}
公立学生下一个(){
学生s=null;
if(studentCount==0)
indexSelectedStudent=-1;
否则{
indexSelectedStudent=Math.min(studentCount-1,indexSelectedStudent+1);
s=学生[索引选定学生];
}
返回s;
}
公立学生最后{
学生s=null;
if(studentCount==0)
indexSelectedStudent=-1;
否则{
indexSelectedStudent=studentCount-1;
s=学生[索引选定学生];
}
返回s;
}
//用于观察数据的访问器
公立学生{
如果(indexSelectedStudent==-1)
返回null;
其他的
返回学生[索引选定学生];
}
公共整数大小(){
返回学生人数;
}
public int currentPosition(){
返回指标selectedstudent;
}
public int getClassAverage(){
if(studentCount==0)
返回0;
整数和=0;
for(int i=0;i
一个arrayList可以包含任何类型的对象,所以你为什么不把你的Student对象放进去,然后根据你的需要访问它呢

private ArrayList<Student> studentList = new ArrayList<Student>();
a) 您应该将其声明为列表,因为您应该使用抽象(即接口),并且只有在实际构建时才使用ArrayList

List<Integer> tests = new ArrayList<Integer>();
List tests=new ArrayList();
b) 使用列表界面的已发布列表作为参考

下面是一个获取列表平均值的示例

public double average(List<Integer> tests) {
  if (tests.isEmpty()) {
    // this return value depends on what you want to do when
    // there's no tests.
    return 0.0;
  }

  // you'll want to use a long here to help avoid overflow
  // since you could reach MAX_INT pretty easily with a large
  // list.
  long sum = 0L;
  for (Integer value : tests) {
    sum += value.longValue();
  }
  // you have to cast to double to allow it to do double arithmetic
  // and actually give you the decimal portion of the answer.
  return (double)sum / (double) tests.size();
}
公共双平均(列表测试){
if(tests.isEmpty()){
//此返回值取决于您需要执行的操作
//没有测试。
返回0.0;
}
//您需要在此处使用long来帮助避免溢出
//因为你可以用一个大的
//名单。
长和=0L;
for(整数值:测试){
sum+=value.longValue();
}
//您必须强制转换为double,以允许它执行双重算术
//给你答案的小数部分。
返回(双)和/(双)测试。大小();
}

不确定你想要什么。您的代码看起来仍然基于数组实现,您刚刚替换了数组的声明。除了“请让它工作”之外还有什么问题吗?请按Ctrl+Shift+F……你的标签到处都是。
public double average(List<Integer> tests) {
  if (tests.isEmpty()) {
    // this return value depends on what you want to do when
    // there's no tests.
    return 0.0;
  }

  // you'll want to use a long here to help avoid overflow
  // since you could reach MAX_INT pretty easily with a large
  // list.
  long sum = 0L;
  for (Integer value : tests) {
    sum += value.longValue();
  }
  // you have to cast to double to allow it to do double arithmetic
  // and actually give you the decimal portion of the answer.
  return (double)sum / (double) tests.size();
}