Array table help

-1 khris microbean · September 14, 2014
I'm on bucky's 28th tutorial. I understand everything but one thing. Why do we have to put the variable(counter) in the array variable when we print the last line?  

int arrayname[] = {10 , 20 , 30 , 40 ,50};

for(int counter = 0;counter<arrayname.length;counter++){

System.out.println(counter + "\t" + arrayname[counter]);

Post a Reply


Oldest  Newest  Rating
+1 Adam Karrer · September 14, 2014
for(int counter = 0;counter<arrayname.length;counter++){

So what we're saying here is:

"Start counter at 0, while the counter is less than the length of the array, do stuff then add one to the counter."

System.out.println(counter + "\t" + arrayname[counter]);

Here we're saying:

"Print out the counter number, then the array at the index of the counter."

So when we loop the first time, counter is 0, which prints arrayname[0] (the first thing in the array), second time we loop, the counter becomes 1, so arrayname[1] (the second thing in the array), etc. etc. until the loop ends.

Hope that helps!
  • 1

Java / Android Development


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

Bucky Roberts Administrator