报错,null [java.lang.IndexOutOfBoundsException,Index: 5, Size: 5]

博客内容涉及一个编程问题,其中在遍历两个列表时,由于循环变量使用错误,导致了`IndexOutOfBoundsException`。作者展示了原始代码中存在错误的循环,并给出了修正后的代码。修正点在于将内部循环的迭代变量`j`的递增操作从`i++`更正为`j++`。这个问题涉及到Java集合操作和异常处理,是编程实践中常见的错误类型。
摘要由CSDN通过智能技术生成

问题:
{
    "success": false,
    "code": "S0000",
    "error": "null [java.lang.IndexOutOfBoundsException,Index: 5, Size: 5]",
    "serviceName": "cols-service",
    "paramErrors": null
}
原因:
HashMap<String, Object> objectHashMap = new HashMap<>();
List<Map<String,String>> mapList= new ArrayList<>();
HashMap<String, String> hashMap = new HashMap<>();
for (int i=0;i<cardParameterInfoList.size();i++){
    Long cardParameterId = cardParameterInfoList.get(i).getCardParameterId();
    String value = cardParameterInfoList.get(i).getValue();
    for (int j=0;j<cardParameterList.size();i++){
        String name = cardParameterList.get(j).getName();
        Long id = cardParameterList.get(j).getId();
        if (name.equals("FITVersion")){
            if (id.equals(cardParameterId)){
                objectHashMap.put(name,value);
            }
        }else{
            if (id.equals(cardParameterId)){
                hashMap.put(name,value);
            }
        }
        mapList.add(hashMap);
    }
}
解决:
HashMap<String, Object> objectHashMap = new HashMap<>();
List<Map<String,String>> mapList= new ArrayList<>();
HashMap<String, String> hashMap = new HashMap<>();
for (int i=0;i<cardParameterInfoList.size();i++){
    Long cardParameterId = cardParameterInfoList.get(i).getCardParameterId();
    String value = cardParameterInfoList.get(i).getValue();
    for (int j=0;j<cardParameterList.size();j++){
        String name = cardParameterList.get(j).getName();
        Long id = cardParameterList.get(j).getId();
        if (name.equals("FITVersion")){
            if (id.equals(cardParameterId)){
                objectHashMap.put(name,value);
            }
        }else{
            if (id.equals(cardParameterId)){
                hashMap.put(name,value);
            }
        }
        mapList.add(hashMap);
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值