The strideList.get(9) will throw a IndexOutOfBoundsException
and will take more time than list.size()
However point to be noted is the code for get(int index)
. There are two steps involved
1. rangeCheck which just matches the size which you get out of size()
2. If the range is greater than size , it throws the Exception,which is additional time taken
3. If the size is not greater than size , then it will get from Array.
Code for ArrayList :
public E get(int index) {
382 rangeCheck(index);
383
384 return elementData(index);
385 }
private void rangeCheck(int index) {
603 if (index >= size)
604 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
605 }
606
E elementData(int index) {
371 return (E) elementData[index];
372 }
373