- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterating over Arrays in Java
Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
To iterate over arrays in Java, you can use the foreach loop −
Example
public class Demo { public static void main(String[] args) { double[] myList = {20.3, 35.7, 50.8, 70.2, 90.8}; // Print all the array elements for (double element: myList) { System.out.println(element); } } }
Output
20.3 35.7 50.8 70.2
You can also iterate over arrays in Java using the for loop −
Example
public class Demo { public static void main(String[] args) { double[] myList = {50.5, 100.2, 200.5, 400.6, 500.5, 700.8, 900.2, 1000.5}; // Print all the array elements for (int i = 0; i < myList.length; i++) { System.out.println(myList[i] + " "); } } }
Output
50.5 100.2 200.5 400.6 500.5 700.8 900.2 1000.5
- Related Articles
- The Iterating over Arrays in Java
- Iterating over array in Java
- Iterating over ArrayLists in Java
- Iterating over Enum Values in Java
- How do you use ‘foreach’ loop for iterating over an array in C#?
- C++ Remove an Entry Using Key from HashMap while Iterating Over It
- C++ Remove an Entry Using Value from HashMap while Iterating over It
- How to avoid ConcurrentModificationException while iterating a collection in java?
- How to iterate over arrays and objects in jQuery?
- Arrays in Java
- Final Arrays in Java
- Assigning arrays in Java.
- Join a sequence of arrays with stack() over specific axis in Numpy
- Join a sequence of arrays with stack() over negative axis in Numpy
- Iterating C# StringBuilder in a foreach loop

Advertisements