xbd-ovo 2024-08-06 19:32 采纳率: 25%
浏览 2
已结题

Java中出现数组角标越界

运行测试代码时:
idea提示NameListService类第49行出现数组越界,然后提示bonus调用的EMPLOYEES[i][5]越界(当我把5改为0便正常运行),此二维数组在Data类中,下面是错误提示处以及Data类,NameListService类的代码:
当然这是在一个switch case的结构里面只有当有bonus时,他才会执行这个语句

img

img

img

package service;

public class Data {
    public static final int EMPLOYEE = 10;
    public static final int PROGRAMMER = 11;
    public static final int DESIGNER = 12;
    public static final int ARCHITECT = 13;

    public static final int PC = 21;
    public static final int NOTEBOOK = 22;
    public static final int PRINTER = 23;

    //Employee  :  10, id, name, age, salary
    //Programmer:  11, id, name, age, salary
    //Designer  :  12, id, name, age, salary, bonus
    //Architect :  13, id, name, age, salary, bonus, stock
    public static final String[][] EMPLOYEES = {                     ** //NameListService中调用EMPLOYEES[i][5]这能越界??????????******
            {"10", "1", "马 云", "22", "3000"},
            {"13", "2", "马化腾", "32", "18000", "15000", "2000"},
            {"11", "3", "李彦宏", "23", "7000"},
            {"11", "4", "刘强东", "24", "7300"},
            {"12", "5", "雷 军", "28", "10000", "5000"},
            {"11", "6", "任志强", "22", "6800"},
            {"12", "7", "柳传志", "29", "10800","5200"},
            {"13", "8", "杨元庆", "30", "19800", "15000", "2500"},
            {"12", "9", "史玉柱", "26", "9800", "5500"},
            {"11", "10", "丁 磊", "21", "6600"},
            {"11", "11", "张朝阳", "25", "7100"},
            {"12", "12", "杨致远", "27", "9600", "4800"}
    };

    //如下的EQUIPMENTS数组与上面的EMPLOYEES数组元素一一对应
    //PC      :21, model, display
    //NoteBook:22, model, price
    //Printer :23, name, type
    public static final String[][] EQUIPMENTS = {
            {},
            {"22", "联想T4", "6000"},
            {"21", "戴尔", "NEC17寸"},
            {"21", "戴尔", "三星 17寸"},
            {"23", "佳能 2900", "激光"},
            {"21", "华硕", "三星 17寸"},
            {"21", "华硕", "三星 17寸"},
            {"23", "爱普生20K", "针式"},
            {"22", "惠普m6", "5800"},
            {"21", "戴尔", "NEC 17寸"},
            {"21", "华硕","三星 17寸"},
            {"22", "惠普m6", "5800"}
    };
}

package service;

import domain.*;
import service.Data;

import static service.Data.*;
/*
功能:负责将Data中的数据封装到Employee[]数组中,同时提供相关操作Employee[]的方法。
说明:
employees用来保存公司所有员工对象
NameListService()构造器:
根据项目提供的Data类(12人)构建相应大小的employees数组
再根据Data类中的数据构建不同的对象,包括Employee、Programmer、Designer和Architect对象,以及相关联的Equipment子类的对象
将对象存于数组中
Data类位于com.atguigu.team.service包中
*/

public class NameListService {
    private Employee[] employees;

    public NameListService() {
        employees = new Employee[Data.EMPLOYEES.length]; //12个人

        for (int i = 0; i < employees.length; i++) {
            //通用属性
            int type = Integer.parseInt(EMPLOYEES[i][0]);  //确定员工类型
            int id = Integer.parseInt(EMPLOYEES[i][1]);
            String name = EMPLOYEES[i][2];
            int age = Integer.parseInt(EMPLOYEES[i][3]);
            double salary =Double.parseDouble(EMPLOYEES[i][4]);

            //子类某些通用属性
            Equipment eq;
            double bonus;
            int stock;

            //将Data中的员工信息封装进NameListService的private Employee[] employees;数组里面
            switch(type){
                case EMPLOYEE://Data中typeEMPLOYEE对应10
                    employees[i]=new Employee(id, name, age, salary);
                            break;
                case PROGRAMMER:
                    eq = createEquipment(i);
                    employees[i]=new Programmer(id, name, age, salary,eq);
                case DESIGNER:
                    eq = createEquipment(i);
                    //是Double不是Integer*********************************************
//EMPLOYEES[i][5]和EMPLOYEES[i][5]为什么会显示数组越界?????????????????????????????????????????????????????/
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    employees[i]=new Designer(id, name, age, salary,eq,bonus);
                case ARCHITECT:
                    eq = createEquipment(i);
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    stock= Integer.parseInt(EMPLOYEES[i][6]);
                    employees[i]=new Architect(id, name, age, salary,eq,bonus,stock);

            }
        }

    }

    private Equipment createEquipment(int i) {
        int type = Integer.parseInt(EQUIPMENTS[i][0]);  //确定设备类型
        String modelOrName = EQUIPMENTS[i][1];
        String priceOrDisplayType= EQUIPMENTS[i][2];
        switch(type){
            case PC :
                return new PC(modelOrName,priceOrDisplayType);
            case NOTEBOOK :
                double price=Double.parseDouble(priceOrDisplayType);
                return new NoteBook(modelOrName,price);
            case PRINTER :
                return new Printer(modelOrName,priceOrDisplayType);
        }
        return null;
    }

    public Employee[] getAllEmployees() {
        return employees;
    }

    public Employee getEmployee(int id) throws TeamException {
        for (int i = 0; i < employees.length; i++) {
            if(employees[i].getId() == id){
                return employees[i];
            }
        }
        throw new TeamException("该员工不存在");
    }

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 已结题 (查看结题原因) 8月6日
    • 修改了问题 8月6日
    • 创建了问题 8月6日

    悬赏问题

    • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
    • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
    • ¥15 QQ邮箱过期怎么恢复?
    • ¥15 登录他人的vue项目显示服务器错误
    • ¥15 (标签-android|关键词-app)
    • ¥60 如何批量获取json的url
    • ¥15 comsol仿真压阻传感器
    • ¥15 Python线性规划函数optimize.linprog求解为整数
    • ¥15 llama3中文版微调
    • ¥15 pg数据库导入数据序列重复