如何处理数组列表异常?

How to deal with arraylist exceptions?

以下代码应从文件中读取学生列表及其成绩,将它们相互比较,然后显示平均分最高的学生

public class CSDept implements Comparable{
private String studentName;
private double java;
private double dataStructure;
private double algorithms;
private int numStudents;


public CSDept(){

}

public CSDept(String studentName,double java,double dataStructure,double algorithms){
    this.studentName=studentName;
    this.java=java;
    this.dataStructure=dataStructure;
    this.algorithms=algorithms;
}

public String getStudentName() {
    return studentName;
}

public void setStudentName(String studentName) {
    this.studentName = studentName;
}

public String getJava() {
    return java+" ";
}

public void setJava(double java) {
    this.java = java;
}

public String getDataStructure() {
    return dataStructure+" ";
}

public void setDataStructure(double dataStructure) {
    this.dataStructure = dataStructure;
}

public String getAlgorithms() {
    return algorithms+" ";
}

public void setAlgorithms(double algorithms) {
    this.algorithms = algorithms;
}


public int getNumStudents() {
    return numStudents;
}

public double getAvg(){
    return (java+algorithms+dataStructure)/3;
}



public int compareTo(Object student) {
    if(this.getAvg()>((CSDept)student).getAvg()) return 1;
    if (this.getAvg()<((CSDept)student).getAvg()) return -1;
    return 0;
}

public String toString(){
    return studentName+":\n"+"\t"+"Java:  "+java+"\t"+"Data Structure :  "+dataStructure+"\t"+"Algorithms:  "+algorithms+"\n";
}}

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.util.*;

    import javax.swing.*;
    import javax.swing.border.TitledBorder;


public class CSDeptFrame extends JFrame{
private JPanel pnlInput=new JPanel(new GridLayout(4,2));
private JPanel pnlOutput=new JPanel(new BorderLayout());
private JPanel pnlFinal=new JPanel(new GridLayout(1,2));

private TitledBorder brdInput=new TitledBorder("Marks");
private TitledBorder brdOutput=new TitledBorder("First Student");

private JLabel lblName=new JLabel("Student Name");
private JLabel lblJava=new JLabel("Java");
private JLabel lblDataStructure=new JLabel("Data Structure");
private JLabel lblAlgorithm=new JLabel("Algorithm");
static JLabel lblFirst=new JLabel("The First Student is :");

static JTextField txtName=new JTextField(20);
static JTextField txtJava=new JTextField(20);
static JTextField txtDataStructure=new JTextField(20);
static JTextField txtAlgorithm=new JTextField(20);

static JButton btnFirst=new JButton("Who is The First Student?");

public CSDeptFrame(String title){
    super(title);
    pnlInput.setBorder(brdInput);
    pnlInput.add(lblName);
    pnlInput.add(txtName);
    pnlInput.add(lblJava);
    pnlInput.add(txtJava);
    pnlInput.add(lblDataStructure);
    pnlInput.add(txtDataStructure);
    pnlInput.add(lblAlgorithm);
    pnlInput.add(txtAlgorithm);

    pnlOutput.setBorder(brdOutput);
    pnlOutput.add(btnFirst,BorderLayout.NORTH);
    pnlOutput.add(lblFirst,BorderLayout.SOUTH);

    pnlFinal.add(pnlInput);
    pnlFinal.add(pnlOutput);

    setLayout(new BorderLayout());
    add(pnlFinal);}

public static void main(String[] args){
    CSDeptFrame frame=new CSDeptFrame("CS DEPT");
    frame.setSize(450,200);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    final ArrayList<CSDept> cS= new ArrayList();
    File readFile =new File("read.txt");
    final File writeFile=new File("write.txt");


    try {
        Scanner scan=new Scanner(readFile);
        while(scan.hasNext()){
            CSDept student=new CSDept();
            student.setStudentName(scan.next());
            student.setJava(scan.nextDouble());
            student.setDataStructure(scan.nextDouble());
            student.setAlgorithms(scan.nextDouble());
            cS.add(student);


        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, "OPS! File is not found");
        }

    btnFirst.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            CSDept firstStudent=new CSDept();
            firstStudent.setStudentName(cS.get(0).getStudentName());
            firstStudent.setJava(Double.parseDouble(cS.get(0).getJava()));
            firstStudent.setDataStructure(Double.parseDouble(cS.get(0).getDataStructure()));
            firstStudent.setAlgorithms(Double.parseDouble(cS.get(0).getAlgorithms()));

            for (int i=0;i<cS.size();i++){
                if (cS.get(i).compareTo(cS.get(i+1))==-1){
                    firstStudent=cS.get(i+1);
                }
            }
            txtName.setText(firstStudent.getStudentName());
            txtJava.setText(firstStudent.getJava());
            txtDataStructure.setText(firstStudent.getDataStructure());
            txtAlgorithm.setText(firstStudent.getAlgorithms());
            lblFirst.setText("The First Student is: "+ txtName.getText());
            PrintWriter out;
            try {
                out = new PrintWriter(new BufferedWriter(new FileWriter(writeFile,true)));
                for (CSDept cs: cS){

                    out.println(cs.toString());
                    out.print("The first student is " + firstStudent.toString());
                }
                 out.close();
            } catch (IOException e1) {
                e1.printStackTrace();
                JOptionPane.showMessageDialog(null, "OPS! File in Not Found");
            }

        }
    });

}}

但问题是下面的语句并没有向arraylist添加任何对象,这是什么问题?

try {
        Scanner scan=new Scanner(readFile);
        while(scan.hasNext()){
            CSDept student=new CSDept();
            student.setStudentName(scan.next());
            student.setJava(scan.nextDouble());
            student.setDataStructure(scan.nextDouble());
            student.setAlgorithms(scan.nextDouble());
            cS.add(student);


        }

您的问题是您正试图从 列表中的特定索引处获取元素:

final ArrayList<CSDept> cS= new ArrayList();

// ...

public void actionPerformed(ActionEvent e) {
    // ...
    firstStudent.setStudentName(cS.get(0).getStudentName()); // ArrayList cS is empty here
    // ...

也许您可以在访问元素之前添加检查以确保列表不为空:

public void actionPerformed(ActionEvent e) {
    if(!cS.isEmpty()) {
        // do stuff
    }

你的列表为空的原因可能是你正在读取一个空文件 "read.txt" 所以你的 while 循环条件永远不会 true

Scanner scan=new Scanner(readFile); // this text file is probably empty
while(scan.hasNext()){              // which makes this condition always false so loop is never executed
    CSDept student=new CSDept();
    student.setStudentName(scan.next());
    student.setJava(scan.nextDouble());
    student.setDataStructure(scan.nextDouble());
    student.setAlgorithms(scan.nextDouble());
    cS.add(student);
}

此外,actionPerformed() 内的 for 循环存在 ArrayIndexOutOfBoundsException 风险。您将当前元素与下一个元素进行比较,一旦 i 对应于最后一个元素的索引,就会在比较语句中给出一个 AIOBE 元素位于索引 i + 1:

for (int i=0;i<cS.size();i++){
    if (cS.get(i).compareTo(cS.get(i+1))==-1){  // once i = cS.size() - 1, you will get an AIOBE here
        firstStudent=cS.get(i+1);
    }
}

您可以通过在 i = 1 处启动循环并与 i - 1 处的元素进行比较或使用 forEach 循环来解决此问题,这是一种更简洁的实现方式,因为它不会不涉及索引访问。