In this Java article we are going to learn How to Print Array using Java For Loop, An array
is a container object that holds a fixed number of values of a single type. The length of an
array is established when the array is created. After creation, its length is fixed.
Also you can check Java GUI Development with JavaFX
1: JavaFX GUI Development Tutorials
In this example we are going to create an integer array, after that we print the array using
Java For Loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Codeloop { public static void main(String[] args) { int Arr[] = new int[]{1,2,3,4,5,6,7,8,9,10}; for(int i=0; i< 10; i++) { System.out.print(Arr[i] +", "); } } } |
In here we have created our array.
1 |
int Arr[] = new int[]{1,2,3,4,5,6,7,8,9,10}; |
We have printed the array using a for loop.
1 2 3 4 |
for(int i=0; i< 10; i++) { System.out.print(Arr[i] +", "); } |
Now if you run the code this will be the result.
1 |
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
Also you can create array from type of String.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Codeloop { public static void main(String[] args) { String Arr[] = new String[]{"Python", "C++", "Java", "Ruby", "C#", "Kotlin"}; for(int i=0; i< 6; i++) { System.out.print(Arr[i] +", "); } } } |
If you run the code this will be the result.
1 |
Python, C++, Java, Ruby, C#, Kotlin, |
Also you can use for each loop to iterate over arrays, it is an enhanced version of the for loop. the Java for-each loop was introduced since J2SE 5.0. and it is another way to iterate over arrays in Java, and the main usage is for traversing the array or collection elements. the best advantage about for-each loop is that it makes code more readable.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Codeloop { public static void main(String[] args) { String programmming[] = new String[]{"Python", "C++", "Java", "Ruby", "C#", "Kotlin"}; for (String pro : programmming) { System.out.println(pro); } } } |
If you run the code this will be the result.
1 2 3 4 5 6 |
Python C++ Java Ruby C# Kotlin |
Subscribe and Get Free Video Courses & Articles in your Email