代码优化之switch语句和数组的妙用
2026/6/8 17:02:56 网站建设 项目流程

一、基本知识复盘

1.数组

(1) 什么是数组

用来存储固定长度相同类型数据的容器

(2) 定义方式

① 声明 + 分配空间(常用)

int[] arr = new int[5];

② 声明 + 直接赋值

int[] arr = {10, 20, 30, 40}; String[] names = {"张三", "李四", "王五"};

③ 先声明,后赋值

int[] arr; arr = new int[]{1,2,3};
(3) 遍历

① 普通 for 循环

for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }

② 增强 for 循环

for (int num : arr) { System.out.println(num); }

2.switch

(1) switch语句

多分支选择语句,适合固定等值判断,相比多层if-else if代码更简洁

(2) 基本语法
switch(表达式) { case 常量1: 代码块1; break; case 常量2: 代码块2; break; // 多个case default: 默认代码块; break; }

default说明:

  • 可选,不是必须写
  • 位置任意,不一定在最后
  • 所有 case 都不匹配才会执行
  • 建议最后也加上break,避免穿透
(3) 支持的类型
  • 基础类型(JDK 1.0+):byteshortintchar

  • 包装类(JDK 5+ 自动装箱):ByteShortIntegerCharacter

  • 字符串(JDK 7+ 新增):String

  • 枚举(JDK 5+ 新增):enum枚举类型

注:switch不支持longfloatdoubleboolean

(4) 使用场景
  • 优先用 switch:等值判断、分支多、固定常量
  • 优先用 if:范围判断(>、<、区间)、复杂逻辑判断
(5) JDK14+ 新特性

增强 switch(箭头语法、表达式)

  • 箭头 case(无穿透,不用写 break)
int num = 2; switch (num) { case 1 -> System.out.println("一"); case 2 -> System.out.println("二"); default -> System.out.println("其他"); }
  • switch 作为表达式(直接返回值)
int num = 3; String res = switch (num) { case 1 -> "第一名"; case 2 -> "第二名"; default -> "其他名次"; }; System.out.println(res);

二、代码优化方案

1.核心思想

  • 数组:统一管理映射关系、常量、批量数据,消除魔法值、重复变量,集中配置便于维护。
  • switch:替代多层if-else if等值分支判断,语法结构清晰、可读性高、执行效率优于长串 if。
  • 组合使用:数组存静态文案 / 配置 / 参数,switch 处理分支业务逻辑,各司其职。

2.优化场景

(1) 数字编码 ↔ 文本描述(最常用,消除魔法值)

此时业务中大量存在:状态码、类型码、错误码对应中文 / 提示语。

反例(硬编码 + 多层 if,维护极差)

public static String getStatusName(int status) { if (status == 1) { return "待审核"; } else if (status == 2) { return "审核通过"; } else if (status == 3) { return "审核驳回"; } else if (status == 4) { return "已作废"; } return "未知状态"; }

问题:新增状态要改 if 分支、文本分散、重复代码多。

优化方案:数组做映射(纯映射,无需 switch)

// 数组下标 = 状态码,值 = 对应文本,集中配置 private static final String[] STATUS_MAPPING = { "未知状态", // 0 "待审核", // 1 "审核通过", // 2 "审核驳回", // 3 "已作废" // 4 }; public static String getStatusName(int status) { // 边界校验,防止数组越界 if (status < 0 || status >= STATUS_MAPPING.length) { return STATUS_MAPPING[0]; } return STATUS_MAPPING[status]; }

优势

  • 配置统一,修改文案只改数组;
  • 时间复杂度 O (1),性能最优;
  • 新增状态仅扩展数组长度。
(2) 等值分支逻辑(if-else 臃肿 → switch 优化)

不同编码不仅要返回文本,还要执行不同业务逻辑,搭配switch拆分分支,数组承载文案配置。

反例(多层 if,逻辑混杂)

public static void handleOrder(int orderStatus) { if (orderStatus == 1) { System.out.println("待付款:提醒用户支付"); // 业务逻辑A } else if (orderStatus == 2) { System.out.println("已付款:通知仓库发货"); // 业务逻辑B } else if (orderStatus == 3) { System.out.println("已发货:更新物流信息"); // 业务逻辑C } else if (orderStatus == 4) { System.out.println("已完成:生成结算单"); // 业务逻辑D } else { System.out.println("未知订单状态"); } }

优化方案:数组存文案 + switch 处理分支逻辑

public class OrderService { // 常量数组:统一管理状态文本(全局静态常量,规范写法) private static final String[] ORDER_STATUS = { "未知订单状态", "待付款", "已付款", "已发货", "已完成" }; public static void handleOrder(int orderStatus) { switch (orderStatus) { case 1: System.out.println(ORDER_STATUS[1] + ":提醒用户支付"); // 专属业务逻辑 break; case 2: System.out.println(ORDER_STATUS[2] + ":通知仓库发货"); // 专属业务逻辑 break; case 3: System.out.println(ORDER_STATUS[3] + ":更新物流信息"); // 专属业务逻辑 break; case 4: System.out.println(ORDER_STATUS[4] + ":生成结算单"); // 专属业务逻辑 break; default: System.out.println(ORDER_STATUS[0]); } } }

进阶:利用 switch 穿透合并相同逻辑

多个编码执行同一套逻辑,省略break实现分支合并,搭配数组复用文案:

// 需求:1/2/3 都属于正常订单,4/5 属于异常订单 public static void checkOrder(int code) { String[] tip = {"未知", "正常订单", "异常订单"}; switch (code) { case 1: case 2: case 3: System.out.println(tip[1] + ",正常流转"); break; case 4: case 5: System.out.println(tip[2] + ",触发告警"); break; default: System.out.println(tip[0]); } }
(3) 非连续编码 / 复杂参数映射(数组 + switch 组合)

实际开发中,状态码不连续(如 101、202、305),无法直接用数组下标映射,此时:

  • 数组存放通用配置 / 固定参数
  • switch 精准匹配离散编码。
// 数组:统一配置通用提示语 private static final String[] MSG = {"操作失败", "操作成功", "权限不足"}; public static void operate(int code) { switch (code) { case 101: System.out.println(MSG[1] + ":新增数据"); break; case 202: System.out.println(MSG[1] + ":修改数据"); break; case 305: System.out.println(MSG[2] + ":禁止操作"); break; default: System.out.println(MSG[0]); } }
(4) 批量数据处理(纯数组优化,搭配循环)

针对重复变量、批量计算、批量操作,用数组替代零散变量,结合循环简化代码,常和分支判断联动。

反例(零散变量,冗余严重)

int s1 = 88; int s2 = 92; int s3 = 79; int total = s1 + s2 + s3; double avg = total / 3.0;

优化(数组 + 循环)

public static void calcScore() { // 数组统一存储批量数据 int[] scores = {88, 92, 79}; int total = 0; for (int score : scores) { total += score; } double avg = (double) total / scores.length; System.out.println("总分:" + total + ",平均分:" + avg); }

结合 switch 做分组统计(综合实战):

// 按分数段分类统计 public static void scoreGroup(int[] scores) { int excellent = 0, good = 0, pass = 0; for (int s : scores) { int level; if (s >= 90) level = 1; else if (s >= 60) level = 2; else level = 3; switch (level) { case 1: excellent++; break; case 2: good++; break; case 3: pass++; break; } } String[] label = {"优秀", "良好", "不及格"}; System.out.println(label[0] + ":" + excellent); System.out.println(label[1] + ":" + good); System.out.println(label[2] + ":" + pass); }

3. 编码规范 & 避坑要点

(1)数组使用规范
  • 状态映射数组定义为private static final全局常量,避免重复创建;
  • 必须做下标越界校验(index >= 0 && index < 数组名.length,防止ArrayIndexOutOfBoundsException
  • 编码连续优先数组,编码离散优先 switch + 数组组合。
(2)switch 使用规范
  • 只用于等值判断,范围判断(>、<、区间)仍用 if;
  • 非特殊场景禁止省略 break,避免意外穿透;
  • 建议保留default分支,兜底异常值;
  • case 后只能是常量,不能使用变量、表达式。
(3)组合使用取舍
  • 仅做编码→文本映射:只用数组,最简单高效;
  • 编码匹配 +多分支业务逻辑:数组存文案,switch 走逻辑;
  • 分支少于 3 个:直接用 if 即可,没必要强行用 switch。
(4)性能对比
  • 数组下标访问:O (1),性能最高;
  • switch:底层查表,性能优于 3 层以上的 if-else if 链;
  • 分支极多场景,数组 > switch > 多层 if。

三、实践

1.第一段代码

/** * 实现对应消息类型是否已经校验通过,常用来对业务流程是否合法进行校验 * */ private boolean verify(RequestInfo request, Map<String, Item> items) { if(request.getAut().equals("02") || request.getAut().equals("03")) { Item item02 = items.get("theMsgOf02"); if(ObjectUtil.isEmpty(item02)){ return false; } String status = item02.get("status"); return "PASS".equals(status) ?true:false; } if(request.getAut().equals("05") ) { Item item05 = items.get("theMsgOf05"); if(ObjectUtil.isEmpty(item05)){ return false; } String status = item05.get("status"); return "PASS".equals(status) ?true:false; } return true; }

使用switch语句(Java7及以上版本)进行了优化,减少了重复代码并提高了可读性:

private boolean verify(RequestInfo request, Map<String, Item> items) { String auth = request.getAut(); Item itemInfo; String verifyStatusKey; switch (auth){ case "02": case "03" verifyStatusKey = "theMsgOf02"; break; case "05" verifyStatusKey = "theMsgOf05"; break; default: return ture; } itemInfo = items.get(verifyStatusKey); if(ObjectUtil.isEmpty(item02)){ return false; } String status = itemInfo.get("status"); return "PASS".equals(status); }

2.第2段代码

private boolean verify(String wltType, Map<String, Item> items){ Item item1 = items.get("msgType1"); if(ObjectUtil.isEmpty(item1)){ return false; } String status1 = item1.getStatus(); Item item2 = items.get("msgType2"); if(ObjectUtil.isEmpty(item2)){ return false; } String status2 = item2.getStatus(); return "PASS".equals(status1) && "PASS".equals(status2); }

通过简化条件判断和消息重复代后的代码

private boolean verify(String wltType, Map<String, Item> items){ // 定义所有格需要验证的状态码 String[] verifyKeys = {"msgType1", "msgType2"}; for(String key : verifyKeys) { Item item = items.get(key); if(ObjectUrtil.isEmpty(item) || !"PASS".equals(item.getStatus())) { return false; } } return true; }

3.第3段代码

String verifyKeys; switch(wltLevel) { case "LEVEL1": String[] passGroup1 = new String[]{"msgType.01.01", "msgType.02.02"}; String[] passGroup2 = new String[]{"msgType.05.01", "msgType.06.00"}; String[] passGroup3 = new String[]{"msgType.11.01", "msgType.12.01"}; String[] passGroup4 = new String[]{"msgType.01.37", "msgType.56.02"}; break; default: String[] passGroup5 = new String[]{"msgType.01.01", "msgType.02.02", "msgType.06.02"}; String[] passGroup6 = new String[]{"msgType.07.01", "msgType.09.02", "msgType.32.02"}; } // Problem:其中任意一组每个msgType类型校验通过则说明对应的Level校验通过 应该怎么实现?

实现任一数组校验通过即为true的需求,可以将每组验证键存储在一个二维数组中,然后遍历这个数组检查每组是否都存在于items中且验证状态符合要求

private boolean verifyGroup(String wltLevel, Map<String, Item> items){ String[][] verifyKeysMatrix; String verifyKeys; switch(wltLevel) { case "LEVEL1": String[] passGroup1 = new String[]{"msgType.01.01", "msgType.02.02"}; String[] passGroup2 = new String[]{"msgType.05.01", "msgType.06.00"}; String[] passGroup3 = new String[]{"msgType.11.01", "msgType.12.01"}; String[] passGroup4 = new String[]{"msgType.01.37", "msgType.56.02"}; break; default: String[] passGroup5 = new String[]{"msgType.01.01", "msgType.02.02", "msgType.06.02"}; String[] passGroup6 = new String[]{"msgType.07.01", "msgType.09.02", "msgType.32.02"}; } for(String[] verifyKeys : verifyKeysMatrix){ boolean groupValid = true; for(String key : verifyKeys) { Item item = items.get(key); if(item == null || !"PASS".equals(item.getStatus())){ // 如果当前组内任一验证失败,则跳出循环检查下一组 groupValid = false; break; } } // 任意一组验证通过即返回true if(groupValid){ return true; } } return false; }

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询