0

why i get error in this code

public class bubble { static void firstLoop(int[] arr1){ int len=arr1.length; for (int x=0; x<=len; x++){ System.out.println(arr1[x]); } System.out.println(" array consist of " + arr1.length + "cells"); } public static void main (String[] args){ int[] arr = {4,5,4,9,10,45,2}; firstLoop(arr); } }

23rd Jan 2019, 4:15 AM
Ivanov Viktor
Ivanov Viktor - avatar
1 Answer
+ 6
You're trying to access the element with index x==arr1.length. Remember that indexes go from 0 to (arr1.length)-1. Change the for loop to this: for (int x=0; x<len; x++){ // Code goes here }
23rd Jan 2019, 4:36 AM
Diego
Diego - avatar