- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 array in Java
Array can be iterated easily using two approaches.
Using for loop - Use a for loop and access the array using index.
Using for-each loop - Use a foreach loop and access the array using object.
Following is an example of using above ways.
Example
public class Tester { public static void main(String[] args) { int[] array = {1,2,3,4,5}; System.out.println("Array: "); //Way 1: for(int i =0; i< array.length; i++){ System.out.println(array[i]); } System.out.println("Array: "); //Way 2: for (int i : array) { System.out.println(i); } } }
Output
Array: 1 2 3 4 5 Array: 1 2 3 4 5
- Related Articles
- Iterating over ArrayLists in Java
- Iterating over Arrays in Java
- The Iterating over Arrays 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
- Iterating through an array, adding occurrences of a true in JavaScript
- How to avoid ConcurrentModificationException while iterating a collection in java?
- What are the different ways to iterate over an array in Java?
- divisibleBy() function over array in JavaScript
- Iterating through a dictionary in Swift
- Advantages of vector over array in C++
- Maximize first array over second in JavaScript
- Iterating C# StringBuilder in a foreach loop

Advertisements