为了账号安全,请及时绑定邮箱和手机立即绑定

将数据存储在多维数组中

将数据存储在多维数组中

慕婉清6462132 2022-08-17 17:26:09
我将此数据存储在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);            }        }}
查看完整描述

1 回答

?
动漫人物

TA贡献1815条经验 获得超10个赞

矩阵意味着您可以使用数组的索引来自 [0-9][0-4]。这就是您收到索引出站异常的原因。您正在尝试访问 。第二个索引值为 5,这不可用。该数组中任何一行的最后一个索引是 。10x5listPatients[0][5]listPatients[0-9][4]

检查计数器值,因为它以某种方式为5。请先修复此值或验证此值,然后再访问该数组的任何索引。


查看完整回答
反对 回复 2022-08-17
  • 1 回答
  • 0 关注
  • 95 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号