Java Program to Iterate Over Arrays Using for and for-each Loop Last Updated : 26 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Java, arrays are a collection of elements of the same type. To access and manipulate elements, we can use iteration techniques such as the for loop and for-each loop (also known as the enhanced for loop).Program to Iterate Over Arrays Using for and for-each Loop in JavaIn the below example, we will demonstrate how to iterate through an array using both the traditional for loop and the simplified for-each loop.Example: Java // Java program to iterate over an array // using for loop and for-each loop public class Loop { public static void main(String[] args) { int[] n = {1, 2, 3, 4, 5}; // Using for loop System.out.println("Using for loop:"); for (int i = 0; i < n.length; i++) { System.out.println(n[i]); } // Using for-each loop System.out.println("Using for-each loop:"); for (int num : n) { System.out.println(num); } } } OutputUsing for loop: 1 2 3 4 5 Using for-each loop: 1 2 3 4 5 Explanation: Here, both loops print the same array elements, but the for-each loop simplifies the iteration by eliminating the need for an index. Comment More infoAdvertise with us Next Article Java Program to Iterate Over Arrays Using for and for-each Loop viv_007 Follow Improve Article Tags : Java Java Programs Java-Arrays Java-Array-Programs Practice Tags : Java Similar Reads Java Program to Iterate Over Characters in String Given string str of length N, the task is to traverse the string and print all the characters of the given string using java. Illustration: Input : str = âGeeksforGeeksâ Output : G e e k s f o r G e e k sInput : str = "GfG" Output : G f G Methods: Using for loops(Naive approach)Using iterators (Opti 3 min read How to Print an Array in Java Without using Loop? Given an array arr in Java, the task is to print the contents of this array without using any loop. First let's see the loop method. Loop method: The first thing that comes to mind is to write a for loop from i = 0 to n, and print each element by arr[i].Pseudo Code: for(int i = 0; i < Array.lengt 2 min read Java Program to Compute the Sum of Numbers in a List Using For-Loop Given a list of numbers, write a Java program to find the sum of all the elements in the List using for loop. For performing the given task, complete List traversal is necessary which makes the Time Complexity of the complete program to O(n), where n is the length of the List. Example: Input : List 2 min read Java Program to Add an Element to ArrayList using ListIterator In this article, we will learn how to add an element to ArrayList using ListIterator. The ListIterator is used to return a list iterator over the list elements. The listIterator() returns an iterator over the list from the beginning, but the listIterator(index) returns an iterator over the list from 2 min read Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r 4 min read Iterate Over the Characters of a String in Java Given string str of length N, the task is to traverse the string and print all the characters of the given string. Illustration: Input : âGeeksforGeeksâ Output : G e e k s f o r G e e k sInput. : âCoderâ Output : C o d e r Methods: Using Naive ApproachUsing String.toCharArray() methodUsing Character 9 min read How to Perform Parallel Processing on Arrays in Java Using Streams? In Java, parallel processing helps to increase overall performance. Arrays may be processed in parallel in Java by using Streams API. When working with big datasets or computationally demanding tasks, this is very helpful. In this article, we will learn how to perform parallel processing on arrays i 2 min read Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem 6 min read Java Program to Increment All Element of an Array by One Given the array, the task is to increment each element of the array by 1. Complete traversal is required for incrementing all the elements of an array. An optimized way to complete a given task is having time complexity as O(N). Examples: Input : arr1[] = {50, 25, 32, 12, 6, 10, 100, 150} Output: ar 4 min read Java Program to Convert String to String Array Given a String, the task is to convert the string into an Array of Strings in Java. It is illustrated in the below illustration which is as follows: Illustration: Input: String : "Geeks for Geeks" Output: String[]: [Geeks for Geeks]Input: String : "A computer science portal" Output: String[] : [A co 6 min read Like