Iterating over Arrays in Java Read Courses Practice Improve Improve Improve Like Article Like Save Article Save Report issue Report Iterating over an array means accessing each element of array one by one. There may be many ways of iterating over an array in Java, below are some simple ways. Method 1: Using for loop: This is the simplest of all where we just have to use a for loop where a counter variable accesses each element one by one. // Java program to iterate over an array // using for loop import java.io.*; class GFG { public static void main(String args[]) throws IOException { int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; int i, x; // iterating over an array for (i = 0; i < ar.length; i++) { // accessing each element of array x = ar[i]; System.out.print(x + " "); } } } Output : 1 2 3 4 5 6 7 8 Method 2: Using for each loop : For each loop optimizes the code, save typing and time. // JAVA program to iterate over an array // using for loop import java.io.*; class GFG { public static void main(String args[]) throws IOException { int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; int x; // iterating over an array for (int i : ar) { // accessing each element of array x = i; System.out.print(x + " "); } } } Output : 1 2 3 4 5 6 7 8 Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule. What We Offer: Comprehensive Course Expert Guidance for Efficient Learning Hands-on Experience with Real-world Projects Proven Track Record with 100,000+ Successful Geeks Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore. Last Updated : 11 Dec, 2018 Like Article Save Article Previous Array Variable Assignment in Java Next Iterating over ArrayLists in Java Share your thoughts in the comments Add Your Comment Please Login to comment...