Using the Enhanced for Loop with array element - Java Language Basics

Java examples for Language Basics:for

Introduction

The enhanced forloop has this format:

for (type identifier : array)
{
    statements...
}

The type identifies the type of the elements in the array.

The identifier provides a name for a local variable that is used to access each element.

Demo Code

public class Main {

  public static void main(String[] args) {
    String[] days = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday" };
    for (String day : days) {
      System.out.println(day);//from  w ww  .  ja v  a 2s  .  c  o m
    }
  }
}

Related Tutorials