An array is a special data structure which you will find in every possible programming language including on scripting language e.g. bash. Array stores elements into the contiguous memory location which means if you know the memory location of one element you can access others, but since Java doesn't allow you to access an array element using memory address, you need to use the index. To learn more about array as a data structure in Java read Data Structures and Algorithm Analysis in Java.
Java Program to Loop Over An Array in Java
Here is our sample Java program to demonstrate how to loop through an array in Java. For simplicity, we have chosen an int array but you can use any type of array, both primitive and reference types. This technique will work for all kind of array, except the multi-dimensional array. In order to loop through the two-dimensional array, you need to use nested loops as shown in the example given here.Though there are several ways to iterate over an array, in this article, I'll show you two of the most common ways, first by using traditional for loop which uses the array index to move forward or backward and the second one using enhanced for loop of Java 5.
There are several differences between looping through an array using for loop and enhanced for loop e.g. first is more flexible, gives more power but also error prone. It gives you an index to track the current element, you can go forward and backward by incrementing or decrementing counter but it also susceptible to one-off error where programmer misses either first or last element of an array.
On the other hand enhanced for loop provides the simplest way to loop through an array. It was originally intended to loop over collection e.g. List, Set or Queue, but you can also use an array with enhanced for loop. It doesn't require the programmer to maintain the counter, instead, it automatically moves from one element to another.
It's best suited for iteration where you want to access each element of array one by one, but it's not as powerful as looping over traditional for loop because you can not jump elements, you cannot traverse backward etc.
Here is our Java program which will teach you how to use both simple for loop and enhanced for loop of Java 1.5 to traverse over an array in Java.
package hello; public class ArrayTester{ public static void main(String[] args) { // how to loop over an integer array int[] primes = {2, 3, 5, 7, 11, 13, 17}; // looping using for loop System.out.println("looping over an array using for loop"); for(int i=0; i< primes.length; i++){ System.out.println("current element is: " + primes[i]); } // looping using enhanced for loop of Java 5 System.out.println("looping over an array using enhanced for loop"); for(int number: primes){ System.out.println(number); } } } Output looping over an array using for loop current element is: 2 current element is: 3 current element is: 5 current element is: 7 current element is: 11 current element is: 13 current element is: 17 looping over an array using enhanced for loop 2 3 5 7 11 13 17
You can see both methods works in the same way for simple iteration. Depending upon your situation and algorithm you may need to traverse array from the last element to backward, you may need to jump over element e.g. printing every second elements etc. For such cases, prefer the traditional for loop. It gives you full control of iteration process to the programmer.
Important points
1) You can use any loop e.g. for, while, and do-while or enhanced for loop to loop over an array.2) If you need a counter to hold the current index of the array to implement your algorithm than use the for loop.
3) If your looping depends upon current element then use while and do-while loop.
4) If you just want to iterate or traverse over all elements of an array then use enhanced for loop. It doesn't need any counter and much more readable than traditional loops.
5) Use traditional for loop if you want full control of looping over an array. It allows both forward and backward traversal and jumping over elements by incrementing the counter.
6) You need to use the nested loops i.e. a loop inside another loop to iterate over a multi-dimensional array in Java.
That's all about how to loop through an array in Java an array in Java. As I said, you have multiple ways to loop over an array, depending upon your requirement and situation you can use any of the mentioned approaches. In short, use for loop if you need to hold the counter which points to current index and uses enhanced for loop if you just want to iterate over all elements of an array. Use while and do-while if your looping depends upon the element of array e.g. stop looping when current element is null etc.
No comments:
Post a Comment