Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Return an Array in Java?
Next article icon

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 + " ");         }     } } 

Output
1 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 + " ");     } } 

Output
1 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++;         }     } } 

Output
1 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 + " "));     } } 

Output
1 2 3 4 5 


Next Article
How to Return an Array in Java?
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Java
  • Java-Array-Programs
  • Java-Arrays
Practice Tags :
  • Java

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
  • Set to Array in Java
    Given a set (HashSet or TreeSet) of strings in Java, convert it into an array of strings. Input : Set hash_Set = new HashSet(); hash_Set.add("Geeks"); hash_Set.add("For"); Output : String arr[] = {"Geeks", "for"} Method 1 (Simple) We simply create an empty array. We traverse the given set and one by
    3 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 Arra
    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 commo
    4 min read
  • Output of Java Programs | Set 47 (Arrays)
    Prerequisite : Arrays in Java Question 1. What is the output of this question? 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
  • Streams on Arrays in Java 8
    In this article, we would be going through stream method of Arrays class which is added in Java 8, it simplifies many operations on arrays as well have improved the efficiency. Addition of different features like lambdas and streams in java 8 have made java efficient to write elegant code which have
    7 min read
  • String Array with Enhanced For Loop in Java
    Enhanced for loop(for-each loop) was introduced in java version 1.5 and it is also a control flow statement that iterates a part of the program multiple times. This for-loop provides another way for traversing the array or collections and hence it is mainly used for traversing arrays or collections.
    1 min read
  • Array Literals in Java
    Literal is just unlikely any constant value that can be assigned to a variable. Illustration: int x = 10; // Here 10 is a literal. Now let us stick to the array literals that are just like primitive and string literals java also allows us to use array literals. Java defines special syntax that allow
    3 min read
  • ArrayDeque add() Method in Java
    The Java.util.ArrayDeque.add(Object element) method in Java is used to add a specific element at the end of the Deque. The function is similar to the addLast() method of ArrayDeque in Java. Syntax: Array_Deque.add(Object element) Parameters: The parameter element is of the type ArrayDeque and refers
    2 min read
  • Array setShort() method in Java
    The java.lang.reflect.Array.setShort() is an inbuilt method in Java and is used to set a specified short value to a specified index of a given object array. Syntax: Array.setShort(Object []array,int index, short value) Parameters: This method takes 3 parameters: array: This is an array of type Objec
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences