Why I'm having a Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at For.main(For.java:10) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why I'm having a Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at For.main(For.java:10)

public class For { public static void main(String[] args) { String [] days = {"Monday", "Tuesday", "Wednesday","Thursday","Friday"}; for (int i = 0; i <= days.length; i++) { System.out.println(days[i]); } } }

21st Dec 2017, 9:00 AM
jomar ibarra
jomar ibarra - avatar
5 Answers
+ 4
Guys thank you very much for your answers, merry christmas guys! Keep safe ☺☺☺
22nd Dec 2017, 12:28 AM
jomar ibarra
jomar ibarra - avatar
+ 3
so basically putting -1 in length attribute will cause the loop to find the end of a certain value? Did I get it right, by the thanks for the reply it help me a lot.
21st Dec 2017, 10:43 AM
jomar ibarra
jomar ibarra - avatar
+ 3
The array.length property gives you the elements's count in the array. But as we know, indexes of the elements starts from 0. So the last element is at index array.length - 1. Using for-loop: for (int i = 0; i < array.length; i++) {} Or for-each loop: for (String day : days) {}
21st Dec 2017, 11:57 AM
Boris Batinkov
Boris Batinkov - avatar
+ 1
i<data's.length
24th Dec 2017, 4:06 AM
邱家洪
邱家洪 - avatar
+ 1
days.length();
27th Dec 2017, 4:00 PM
Md Robiul Suni
Md Robiul Suni - avatar