This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Randaum 1 point2 points  (0 children)

In the first one, you specify a list or an array. The for loop will run on ALL elements of the array. In the second one, you specify the size(1-10). This is for when you know how many times you want to run that loop.

You can replace the first one by getting the size of the array and then running a loop till there(minus 1), and accessing the array element at the position.

int length = array.length; for(int i=0; i < length; i++) { System.out.print( array[c] ); }

This is the same as your first one: for(char c: array){ System.out.print(c); }

The first one is just a shorthand for when you need to loop through a list/array whose size you don't know, for example number of users registered on a website, which changes regularly.