Skip to content

Instantly share code, notes, and snippets.

@madevelopers
Created June 26, 2015 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madevelopers/f5c10a802ec1848e8270 to your computer and use it in GitHub Desktop.
Save madevelopers/f5c10a802ec1848e8270 to your computer and use it in GitHub Desktop.
How to do I get the current index in a forEach loop in Java #java #flashcard

You can't. You need to keep track of the index.

int[] nums = new int[]{1, 2, 3, 4, 5};

int index = 0;
for (int i: nums) {
	System.out.println(index + ": " + i);			
	index++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment