Skip to content

Instantly share code, notes, and snippets.

@mkdika
Last active October 31, 2017 06:55
Show Gist options
  • Select an option

  • Save mkdika/02cadbfeb9a867e0604674ca585b2e37 to your computer and use it in GitHub Desktop.

Select an option

Save mkdika/02cadbfeb9a867e0604674ca585b2e37 to your computer and use it in GitHub Desktop.
For vs For Each
public class ArrayForEach {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// using regular FOR
for (int i = arr.length-1 ; i >= 0 ; i--) {
System.out.print(arr[i] + " ");
}
System.out.println("");
// using FOR EACH
int e=0;
for (int nilai : arr) {
System.out.print(nilai + " ");
e++;
}
System.out.println(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment