0 Elijah Flowers · October 18, 2015
after "bucky" why did we put " [ ]  " around counter instead of () or {}.

Post a Reply


Oldest  Newest  Rating
0 Bjarne Betjent · October 19, 2015
I'm not sure which video you're referring to, but I assume it's one with an array and for loop, right? 

Bucky is the name of the array, which was created before the for loop. 

int bucky[ ] = {5, 10, 15, 20}

System.out.prinln(bucky[0]);  <-- this would now print out 5, because the int we placed in the index 0 in the array is 5. 

System.out.prinln(bucky[1]):  <-- this would print out 10 etc. 

In the for loop, he created a temporary variable, "counter" and set it to 0. Then he say as long as the counter is less than the  length of the array, add 1 to the counter (counter++) and go inside the for loop. 
for(int counter = 0; counter < bucky.length; counter++)

bucky[counter] will while we are inside the for loop, refer to the integer we have stored in the index that equals the value of counter. 
So the first time through the loop, counter = 0. bucky[counter] will then be 5. 

Second time through counter will change to 1. bucky[counter] will then refer to the second integer in our array, which is 10. 

I can add something in the end here in case you didn't know already. 
The indexes for an array starts at 0. So the first element will be placed in the index 0.

That's why if -> int bucky[ ] = {2,3,4};
Then bucky[0] = 2.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator