Java - Loop Through an Array Last Updated : 02 Dec, 2024 Comments Improve Suggest changes Like Article Like Report In Java, looping through an array or Iterating over arrays means accessing the elements of the array one by one. We have multiple ways to loop through an array in Java. Example 1: Here, we are using the most simple method i.e. using for loop to loop through an array. Java // Java program to loop through // an array using for loop import java.io.*; class GFG { public static void main(String args[]){ // taking an array int a[] = { 1, 2, 3, 4, 5 }; int i, x; // Iterating over an array for (i = 0; i < a.length; i++) { // accessing each element of array x = a[i]; System.out.print(x + " "); } } } Output1 2 3 4 5 Example 2: The another method is using enhanced for loop to iterate over arrays in Java. It is same as for loop where rather than using index we directly iterate on elements. Java // Java Program to iterate over arrays // using enhanced for loop import java.io.*; public class GFG { public static void main(String[] args) { // Array Created int a[] = { 1, 2, 3, 4, 5 }; // Iterating using for-each or // enhanced for loop for (int i : a) System.out.print(i + " "); } } Output1 2 3 4 5 Example 3: We can also loop through an array using while loop. Although there is not much of a difference in for and while loop in this condition. While loop is considered effective when the increment of the value depends upon some of the condition. Java // Java program to iterate over an array // using while loop import java.io.*; class Main { public static void main(String args[]) { // taking an array int a[] = { 1, 2, 3, 4, 5 }; int i = 0; // Iterating over an array // using while loop while (i < a.length) { // accessing each element of array System.out.print(a[i] + " "); i++; } } } Output1 2 3 4 5 Example 4: We can also use the Arrays.stream() method. This Method is used to convert an Array into stream and then we can traverse stream using forEach() loop. Java // Java Program to iterate over arrays // using Arrays.stream() Method import java.util.*; public class GFG { public static void main(String[] args) { // Array Created int[] a = { 1, 2, 3, 4, 5 }; // Iteration using Arrays.stream() Arrays.stream(a).forEach(i -> System.out.print(i + " ")); } } Output1 2 3 4 5 Comment More infoAdvertise with us Next Article Java - Loop Through an Array N Nikita tiwari Improve Article Tags : Misc Java Java-Arrays Java-Array-Programs Practice Tags : JavaMisc Similar Reads How to Return an Array in Java? An array is a data structure that consists of a group of elements of the same data type such that each element of the array can be identified by a single array index or key. The elements of the array are stored in a way that the address of any of the elements can be calculated using the location of 5 min read String Arrays in Java A String Array in Java is an array that stores string values. The string is nothing but an object representing a sequence of char values. Strings are immutable in Java, this means their values cannot be modified once created.When we create an array of type String in Java, it is called a String Array 5 min read Reverse an Array in Java Reversing an Array is a common task in every programming language. In Java, there are multiple ways to reverse an array. We can reverse it manually or by using built-in Java methods. In this article, we will discuss different methods to reverse an array with examples.Let us first see the most common 4 min read Output of Java Programs | Set 47 (Arrays) Prerequisite : Arrays in Java Question 1. What is the output of this question? JAVA class Test1 { public static void main(String[] args) { int arr[] = { 11, 22, 33 }; for (int i = 0; i < arr.length; i++) System.out.print(arr[i] + " "); System.out.println(); int arr2[] = new int[3]; arr2 2 min read Output of Java Programs | Set 38 (Arrays) Prerequisite : Arrays in Java Question 1. What is the output of this question JAVA class Test1 { public static void main(String[] args) { int arr[] = { 11, 22, 33 }; System.out.print(arr[-2]); } } Option A) 11 33 B) Error C) exception D) 11 -33 Output: C Explanation : We will get java.lang.ArrayInde 3 min read Like