Skip to content

This code throws an ArrayIndexOutOfBoundsException because the loop iterates one time too many. The loop condition should be i < arr.length

Notifications You must be signed in to change notification settings

Bug-Hunter-X/ArrayIndexOutOfBoundsException-in-Java-for-loop-208eh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Java ArrayIndexOutOfBoundsException Bug

This repository demonstrates a common Java programming error: an ArrayIndexOutOfBoundsException caused by an off-by-one error in a for loop.

The BuggyArray.java file contains the buggy code. The loop iterates one element beyond the array's bounds, leading to the exception.

The FixedArray.java file provides the corrected code. The loop condition is changed to ensure the loop stops before accessing an invalid index.

This example highlights the importance of carefully considering loop boundaries when working with arrays in Java.

About

This code throws an ArrayIndexOutOfBoundsException because the loop iterates one time too many. The loop condition should be i < arr.length

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages