getting exception while putting list data into the map

后端 未结 2 1417
执笔经年
执笔经年 2021-01-29 15:53

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 i

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-29 16:47

    Arrays are zero indexed. e.g. a 100-element array will actually have valid index range of [0,99]. I'm assuming this happens on the line p = pairList.get(i);

    You need to implement some logic to make sure that you're not overstepping the bounds. You're already using an iterator to iterate through pairList, which would protect you from this, but you're also mixing in the index i, which defeats the purpose of the iterator.

    At the very least, include a check before you try to access an element. Something like:

    if (i >= pairList.size())
        break;
    

提交回复
热议问题