getting exception while putting list data into the map

后端 未结 2 1418
执笔经年
执笔经年 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:27

    Your problem is here:

    p = pairList.get(i);
    

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

    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题