Java Array for each loop
In this chapter you will learn:
- How to use for each loop with Java array
- Syntax for array for each loop
- Example - Java array for each loop
Description
We can use a collection-based for loop as an alternative to the numerical for loop when processing the values of all the elements in an array.
Syntax
The syntax of for each loop for an array is as follows.
for(arrayType variableName: array){
process each variableName
}
Example
Java array for each loop
public class Main {
public static void main(String args[]) {
int days[] = {1, 2, 3,};
for(int i:days){
System.out.println(i);//w w w. j av a2 s . c om
}
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- How is the multidimensional arrays stored
- Syntax for Java Multidimensional Arrays
- Example - Java Multidimensional Arrays
- Example - How to create a three-dimensional array
- What are Jagged array
- Example - Jagged array
- How to initialize multidimensional arrays during declaration