if (!dataMap.containsKey(d)) { tb.setV1("0.000"); tb.setV2("0.000"); tb.setV3("0.000"); tb.setEndTime(finalDateList.get(Integer.parseInt(d))); finalList.add(tb); } else if (finalList == null) { tb.setEndTime(finalDateList.get(Integer.parseInt(d))); finalList.add(tb); } java.lang.IndexOutOfBoundsException: Index: 12, Size: 12 如何避免
时间: 2024-02-14 10:08:28 浏览: 18
这个异常通常是由于数组或列表访问超出其范围引起的。要避免这个异常,你需要仔细检查你的代码并确保你在访问数组或列表元素时不会超出其范围。
在你的代码中,出现该异常的原因可能是 finalDateList 的大小为 12,但你尝试访问第 13 个元素。你可以通过在使用 finalDateList 之前检查其大小来避免这个异常,比如:
if (finalDateList.size() > Integer.parseInt(d)) {
tb.setEndTime(finalDateList.get(Integer.parseInt(d)));
finalList.add(tb);
}
这样,如果 finalDateList 的大小不足以访问所需的元素,则不会执行添加到 finalList 的代码。
相关问题
dateList.forEach(d -> { /** * 判断dataMap集合中是否包含了键为 d 的元素 */ if (!dataMap.containsKey(d)) { TBmwyResolvedevicedata tb = new TBmwyResolvedevicedata(); tb.setV1("0.000"); tb.setV2("0.000"); tb.setV3("0.000"); tb.setEndTime(finalDateList.get(Integer.parseInt(d))); finalList.add(tb); } }); dateList集合长度为12 避免下标越界错误
可以在遍历`dateList`之前,先判断`dateList`的长度是否大于0,如果大于0再进行遍历。以下是示例代码:
```java
if (dateList.size() > 0) {
dateList.forEach(d -> {
/**
* 判断dataMap集合中是否包含了键为 d 的元素
*/
if (!dataMap.containsKey(d)) {
TBmwyResolvedevicedata tb = new TBmwyResolvedevicedata();
tb.setV1("0.000");
tb.setV2("0.000");
tb.setV3("0.000");
tb.setEndTime(finalDateList.get(Integer.parseInt(d)));
finalList.add(tb);
}
});
}
```
在上面的代码中,首先判断`dateList`的长度是否大于0,如果大于0再进行遍历。这样就可以避免下标越界错误。
Map<String, OrganizationEo> organizationEoMap = comDataComponent.getAllOrganizationMap(); Map<String, UserEo> userEoMap =comDataComponent.getUserMapByUid(); String code = ""; SchemaColumnRuleResponseDto responseDto = new SchemaColumnRuleResponseDto(); BeanUtils.copyProperties(requestDto,responseDto); //用户是否存在 if(!CollectionUtils.isEmpty(requestDto.getColumnPermissionUserList())){ for (String userUid : requestDto.getColumnPermissionUserList()) { if(!userEoMap.containsKey(userUid)){ code = Message.NOT_EXIST.getCode(); responseDto.setResultCode(code); responseDto.setResultMsg(userUid+messageInfo.getMessageByCode(code)); return responseDto; } } schemaColumnRuleEo.setColumnPermissionUserList(JSONObject.toJSONString(requestDto.getColumnPermissionUserList())); } //用户组织是否存在 if(!CollectionUtils.isEmpty(requestDto.getColumnPermissionOrgList())){ List<String> orgList = requestDto.getColumnPermissionOrgList(); for (String orgCode : orgList) { if(!organizationEoMap.containsKey(orgCode)){ code = Message.NOT_EXIST.getCode(); responseDto.setResultCode(code); responseDto.setResultMsg(orgCode+messageInfo.getMessageByCode(code)); return responseDto; } } schemaColumnRuleEo.setColumnPermissionOrgList(JSONObject.toJSONString(orgList)); } return new SchemaColumnRuleResponseDto();代码优化
可以尝试优化代码,将重复的逻辑封装成方法,提高代码的复用性和可读性,示例代码如下:
```
Map<String, OrganizationEo> organizationEoMap = comDataComponent.getAllOrganizationMap();
Map<String, UserEo> userEoMap = comDataComponent.getUserMapByUid();
String code = "";
SchemaColumnRuleResponseDto responseDto = new SchemaColumnRuleResponseDto();
BeanUtils.copyProperties(requestDto, responseDto);
// 验证用户是否存在
if (!CollectionUtils.isEmpty(requestDto.getColumnPermissionUserList())) {
validateExistence(requestDto.getColumnPermissionUserList(), userEoMap, responseDto, messageInfo, Message.NOT_EXIST);
schemaColumnRuleEo.setColumnPermissionUserList(JSONObject.toJSONString(requestDto.getColumnPermissionUserList()));
}
// 验证组织是否存在
if (!CollectionUtils.isEmpty(requestDto.getColumnPermissionOrgList())) {
validateExistence(requestDto.getColumnPermissionOrgList(), organizationEoMap, responseDto, messageInfo, Message.NOT_EXIST);
schemaColumnRuleEo.setColumnPermissionOrgList(JSONObject.toJSONString(requestDto.getColumnPermissionOrgList()));
}
return responseDto;
// 封装验证存在方法
private static void validateExistence(List<String> idList, Map<String, ?> dataMap, SchemaColumnRuleResponseDto responseDto,
MessageInfo messageInfo, Message notExistMessage) {
for (String id : idList) {
if (!dataMap.containsKey(id)) {
String code = notExistMessage.getCode();
responseDto.setResultCode(code);
responseDto.setResultMsg(id + messageInfo.getMessageByCode(code));
return;
}
}
}
```