DSA-Bootcamp-Java icon indicating copy to clipboard operation
DSA-Bootcamp-Java copied to clipboard

ArrayIndexOutOfBoundsException in BinarySearch >> InfiniteArray

Open oseamiya opened this issue 3 years ago • 4 comments

In this code of Infinite Array from BinarySearch lecture,

When we try to get target element more than half size of the array (like 100 or 130), it will throw ArrayIndexOutOfBoundsException like :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 13 out of bounds for length 11
	at Main.ans(InfiniteArray.java:17)
	at Main.main(InfiniteArray.java:8)

It is because when executing while(target > arr[end]), as end will be greater than size of the array, it will throw this error.

Or, Do we not consider this a bug as we are imagining an infinite array with no limit of the elements ?

oseamiya avatar May 05 '22 09:05 oseamiya

I had the same doubt but apparently Screenshot 2022-05-12 at 1 09 49 PM So yes, we do not consider this a bug as we are imagining an infinite array with no limit of the elements

rishabh-gurbani avatar May 12 '22 07:05 rishabh-gurbani

You can check PR #565

rishabh-gurbani avatar May 12 '22 07:05 rishabh-gurbani

In this code of Infinite Array from BinarySearch lecture,

When we try to get target element more than half size of the array (like 100 or 130), it will throw ArrayIndexOutOfBoundsException like :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 13 out of bounds for length 11
	at Main.ans(InfiniteArray.java:17)
	at Main.main(InfiniteArray.java:8)

It is because when executing while(target > arr[end]), as end will be greater than size of the array, it will throw this error.

Or, Do we not consider this a bug as we are imagining an infinite array with no limit of the elements ?

Since this is an infinite array there is no chance of getting an exception like this, if you still get it, that means the array has a limit and it is not an infinite array :)

nesarptr avatar Jun 20 '22 11:06 nesarptr