Loop vs Enhanced Loop in JAVA | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Loop vs Enhanced Loop in JAVA

i tried to print by using enhanced loop but its show outbounding exception error int marks[] ={10,20,30,40,50}; for(int i : marks) { System.out.println(marks[i])l }

7th Apr 2021, 10:48 AM
Sachin Saxena
Sachin Saxena - avatar
2 Answers
+ 3
marks[i] would mean marks[10] which dosent exist and is out of bounds. Your looking for System.out.println(i) Each loop "i" holds one elememt of the array
7th Apr 2021, 1:57 PM
D_Stark - avatar
+ 1
System.out.println(i) is enough. marks[i] means you try to get 10th,20th... element. But there no such elements. So the error occur. Delete duplicate question https://www.sololearn.com/discuss/2748560/?ref=app
7th Apr 2021, 10:52 AM