Swift Swift - 16 days ago 5
Java Question

IndexOutOfBoundException while adding in queueList

private void removeQueue(Queue queue)
{
queue.setTodoDeleted(false);
if ( queueIndex != -1) {
this.queueList.add(queueIndex , queue);
mItemManger.closeAllItems();
}
}


where queueList is ArrayList of
queue
ArrayList
queuelist
.

Problem : Index is 2 and list size is 1 while adding in the queueList so,
IndexOutOfBoundException
throws at Line

this.queueList.add(queueIndex , queue)
.

What is the best way to avoid this? In Advanced, thanks

Answer

To avoid IndexOutOfBounds issue , Check the actual size and index that you are going to use before performing the operation.

if ( queueList.size()>queueIndex)
{
   this.queueList.add(queueIndex , queue);
}
// Add else part if needed

SO documentation Link : ArrayIndexOutOfBoundsException

Comments