while ((line = br.readLine()) != null) { totalNumber++; log.info("文件处理中--------line={}",line); strings = line.split("\\~\\!\\^"); if(strings == null || strings.length == 0){ break; } FinancialProductNotifyDto financialProductNotifyDto = new FinancialProductNotifyDto(); financialProductNotifyDto.setCustomerNo(strings[5]); String financialProductMsg = FinancialProductNotifyDto.FinancialProductEnum.getFinancialProductMsg(strings[2]); if(StringUtils.isEmpty(financialProductMsg)){ financialProductNotifyDto = null; continue; } String format = String.format(financialProductMsg, strings[3], strings[4]); financialProductNotifyDto.setNotifyMsg(format); list.add(financialProductNotifyDto); }减少循环中break和continue语句的总数,最多使用一条。
时间: 2024-02-14 20:13:22 浏览: 16
可以使用一个 if-else 语句来代替 break 和 continue 语句。例如:
```
while ((line = br.readLine()) != null) {
totalNumber++;
log.info("文件处理中--------line={}", line);
strings = line.split("\\~\\!\\^");
if (strings != null && strings.length > 0) {
FinancialProductNotifyDto financialProductNotifyDto = new FinancialProductNotifyDto();
financialProductNotifyDto.setCustomerNo(strings[5]);
String financialProductMsg = FinancialProductNotifyDto.FinancialProductEnum.getFinancialProductMsg(strings[2]);
if (!StringUtils.isEmpty(financialProductMsg)) {
String format = String.format(financialProductMsg, strings[3], strings[4]);
financialProductNotifyDto.setNotifyMsg(format);
list.add(financialProductNotifyDto);
}
}
}
```
这样,当字符串数组 strings 为空或长度为 0 时,直接跳过当前迭代;否则,根据字符串数组中的元素构造 FinancialProductNotifyDto 对象,在 financialProductMsg 不为空的情况下,将其格式化后设置为 notifyMsg 并加入到 list 中。这种方式不仅减少了 break 和 continue 语句的使用,还使得代码更加简洁易懂。