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
Your problem is here:
p = pairList.get(i);
If your list size is 100, then allowed indexes are between 0 - 99.
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;