+ 1
Can someone please explain me arrays
In this code , why do we write arr[i] in line 7 whereas it is not an array index https://code.sololearn.com/c3gkQhFWkL2w/?ref=app
3 Answers
+ 5
You have a for loop:
for(int i = 0; i < arr.length, i++)
--> i changes its value from 0 to 2
--> arr.length = size of your array (3 values inside: length = 3)
--> array index start at 0, last index = length-1
i = 0: arr[0] = 54
i = 1: arr[1] = 6
i= 2: arr[2] = 8
end of loop is reached (next value would be 3 but your value have to be smaller than 3)
sum+=arr[i]
sum = sum + arr[0] //54
sum = sum + arr[1] //60
sum = sum + arr[2] //68
An array is like a Drawer. Each subject has an index. With a for loop you can read all values in the subjects.
You can create diffrent arrays:
String[] arr = new String[10]
--> an empty String array (length = 10)
char[] arr = {'a', 'b', 'c')
--> a filled char array (length = 3)
+ 1
Thank you Denise RoĂberg
+ 1
Your welcome :)