将列表数据放入地图时获取异常 [英] getting exception while putting list data into the map

查看:110
本文介绍了将列表数据放入地图时获取异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迭代一个列表,并把它放在地图上,但问题是,当我返回那个地图时,我想要一个例外,你可以告诉它背后的原因是什么。我正在获取java。 lang.IndexOutOfBoundsException:索引:100,大小:100,因为我的列表大小是100

I am iterating over a list and and putting it contents over the map but the problen is that when I am returning that map I am geeting an exception could you please advise what is the reason behind it ..I am getting java.lang.IndexOutOfBoundsException: Index: 100, Size: 100 as my list size is 100

for(Map.Entry<Long, Integer> entries : result.entrySet()) {
    pairList.add(new Pair(entries.getKey(), entries.getValue()));
}

int sum = 0;
int min = -1;
int max = -1;

int i = 0;
Pair p = pairList.get(i);

@SuppressWarnings("rawtypes")
//Iterator iterator = pairList.iterator();
Iterator iterator = pairList.listIterator();

while (iterator.hasNext()) {
    if(sum == 0){
        min = (int) p.key;
        max = (int) p.key;
    }
    sum += p.value;
    if(sum < BARRIER) {
        max = (int) p.key;
    } else {
        if(sum > BARRIER) {
            i--;
        } else {
            max = (int) p.key;
        }
        System.out.println(min+"_"+max);
        result1.put(min,max);

        sum = 0;
    }
    i++;
    p = pairList.get(i);
}

System.out.println("#########");

System.out.println(result1.size());
for (Map.Entry<Integer, Integer> entry : result1.entrySet()) {
    System.out.println(entry.getKey() + "/" + entry.getValue());
}

请告知如何克服这个问题

folks please advise how to overcome from this

推荐答案

你的问题在这里:

p = pairList.get(i);

如果您的列表大小为100,则允许的索引介于0到99之间。

If your list size is 100, then allowed indexes are between 0 - 99.

这篇关于将列表数据放入地图时获取异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆