- 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
Java program to iterate over arrays using for and foreach loop
In the article, the users will learn how to iterate the arrays for each loop and for loop with the examples. Create an array with the data that have any type of the data like numeric or statement form in the array. Let’s announce any string array known as designations, and initialize the array with the items as input like this.
Then the users have to iterate this array with the loops and print each element.
for(String designation: designations){}
The users affirm a loop variable. The users have been given a string array because each element is a string In this array so the users must assert the variable. Here the given word designation is the name of the array.
To show you some instances
Instance 1
Suppose the array as shown below −
Input: String[] designations={“Ravi”,”Riya”,”Ashish”}; for(String name: designations) Output: “Ravi”,”Riya”,”Ashish”
Instance 2
Suppose the array as shown below −
Input: int[] designations={2,4,5,7}; for(int numbers: designations) Output: {2,4,5,7}
Multiple Approaches
The users have divided the two approaches like this −
By using ArrayList
Without using the user-defined method
Note − These programs might not offer the expected consequence on any online Java compiler. Online editors may not access your local organization’s file track.
Approach 1: By using ArrayList
The users can display the array with two types of data which are strings and integers to represent the array. ArrayList associated with asList() function is used to generate the string type elements in the array.
Algorithm
Step 1 − begin
Step 2 − take the number matrix
Step 3 − Use the asList() function that the java imports and it will create the list of the string in the array
Step 4 − Use for each loop syntax to traverse the string elements of the array
Step 5 − print the result
Example
// import the java packages import java.util.ArrayList; import java.util.Arrays; import java.util.List; //Take a class called Main public class Main{ //Define the main function public static void main(String[] args){ //Make any list of the array List<String>tc=new ArrayList<>(Arrays.asList("Microsoft","Google","Facebook","Oracle")); System.out.println("Restate the list with for loop"); // using for loop to iterate the array for(int p=0;p<tc.size();p++){ System.out.println(tc.get(p)); } System.out.println("Restate the list with for each loop"); //iterate the array using each loop for(String gdt: tc){ System.out.println(gdt); } } }
Output
Restate the list with for loop Microsoft Google Facebook Amazon Restate the list with for each loop Microsoft Google Facebook Amazon
Approach 2: Without using User-defined Method
The users may utilize for loop to print the different types of the array with any variable. The users may increment and decrement the statement which is also mandatory It begins with the keyword with for loop. The users may utilize the loop mutable that is produced instead of utilizing any indexed array item.
Algorithm
Step 1 − begin
Step 2 − Declare an integer array.
Step 3 − Take the variable o and take the length of the item using for loop
Step 4 − Use for each loop syntax taken the variable called varbl to print the output as the variable.
Step 5 − print the result
Syntax
for(type var: array) { // statements }
Example
//Take a class called Main public class Main{ //Define the main function public static void main(String[] args){ //Let’s make an array with different elements in integers int[] item={2,34,51,8,56,90}; //Print the statement System.out.println("Using for loop, restate the array"); // using for loop for(int o=0;o<item.length;o++){ System.out.println(item[o]); } //Print the statement System.out.println("Using for each loop, restate the array"); // use for each loop syntax for(int varbl: item){ // where varbl is known as a variable //Print the statement System.out.println(varbl); } } }
Output
Using for loop, restate the array 2 34 51 8 56 90 using for each loop, restate the array 2 34 51 8 56 90
Conclusion
In this article, the users explored the approaches to iterate over arrays for each and for loop using Java programming language. Whether the users want any type of index so the users may utilize it for loop, or else it will be better to utilize it for each loop. The first example uses the ArrayList to store the elements of string type whereas the second example depicts the integer type array and traverses the array elements by using for and for each loop.