我将此数据存储在 10x5 矩阵中,但是当我到达第一行的 4 个位置时,出现此错误“线程中的异常”AWT-EventQueue-0“java.lang.ArrayIndexOutOfBoundsException: 5强>”。我认为错误出现在 listPatients[0][counter] 中,但我不知道该怎么办。
public class PatientForm extends javax.swing.JFrame {
Patient[][] patientList;
int counter;
public PatientForm() {
initComponents();
patientList = new Patient[10][5];
counter = 0;
}
private void btnasignarActionPerformed(java.awt.event.ActionEvent evt) {
if (counter < listPatients.length) {
String identification = txtidentification.getText();
String name= txtname.getText();
String lastName = txtlastName.getText();
String eps = txteps.getText();
boolean type = jrbtipo1.isSelected();
String diagnosis = txtdiagnostico.getText();
Patient objPatient = new Patient(identification, name, lastName, eps, type, diagnosis);
listPatients[0][counter] = objPatient;
counter++;
JOptionPane.showMessageDialog(this, "Head" + counter + " Patients.");
}else {
JOptionPane.showMessageDialog(this, "Error", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
最佳答案
10x5
矩阵意味着您可以使用 [0-9][0-4] 之间的数组索引。这就是您收到 IndexOutOfBound 异常的原因。
您正在尝试访问 listPatients[0][5]
。第二个索引值为5,不可用。该数组任意行的最后一个索引是 listPatients[0-9][4]
。
检查你的计数器值,因为不知何故它是 5。在访问该数组的任何索引之前修复此值或验证此值。
关于java - 将数据存储在多维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55215646/