Array Traversal :
- Array traversal means accessing the array element one by one.
- We use looping statements to traverse a array.
- The commonly used loops are for , enhanced for and while loop.
Traversal using for loop :
Example:
public class For_LoopTraversal {
public static void main(String[] args) {
int[] a= {10,90,56,78,82,22,45,23};
System.out.println("Traversing the Array using for loop");
System.out.println();
for(int i=0;i<a.length;i++)
{
System.out.println("Element no "+ (i+1) + "
is :"+a[i]);
}
}
}
Output:
Traversing the Array using for loop
Element no 1 is :10
Element no 2 is :90
Element no 3 is :56
Element no 4 is :78
Element no 5 is :82
Element no 6 is :22
Element no 7 is :45
Element no 8 is :23
Explanation :
- The initial value of loop is 0.
- The given condition is i < a.length , so the loop runs till a.length which is 8 = > 8.
- The loop runs for 8 times.
- i++ increments the i value by 1.
- a[i] is used to access the value in that specified index.
Traversal using While loop :
Example:
public class WhileLoop_Traversal {
public static void main(String[] args) {
int[] a= {10 , 20 , 30 , 40 , 50};
int i=0;
System.out.println("Array Traversal using While Loop");
System.out.println();
while(i < a.length)
{
System.out.println("Element no "+ (i+1) + " is :"+a[i]);
i++;
System.out.println();
}
}
}
Output:
Array Traversal using While Loop
Element no 1 is :10
Element no 2 is :20
Element no 3 is :30
Element no 4 is :40
Element no 5 is :50
Explanation :
- The initial value of loop is 0.
- The given condition is i < a.length , so the loop runs till a.length which is 5 = > 5.
- The loop runs for 5 times.
- i++ increments the i value by 1.
- a[i] is used to access the value in that specified index.
Traversal with Enhanced For Loop :
- It is used to access each element directly without using an index.
- It is mainly used when we only need to read the elements.
Syntax:
for(dataType variable : arrayName) {
// Statements
}
Example:
public class Enhanced_For {
public static void main(String[] args) {
String[] animals= {"Tiger" , "Lion" , "Zebra" , "Monkey" , "Cheetah" , "Black Panther" , "Elephant" , "Deer"};
System.out.println("*********************************");
System.out.println("Traversal using Enhanced For Loop ");
System.out.println("*********************************");
System.out.println();
for(String animal : animals)
{
System.out.println("- " +animal);
System.out.println();
}
}
}
Output:
*********************************
Traversal using Enhanced For Loop
*********************************
- Tiger
- Lion
- Zebra
- Monkey
- Cheetah
- Black Panther
- Elephant
- Deer
Explanation:
- String –> Data type of array elements.
- animal –> Temporary variable.
- animals – Array name.
- In each iteration, one element is assigned to animal.
Traversal of 2D Array Using Nested for Loop:
- To traverse a two-dimensional array, we use nested loops.
- The outer loop is used to iterate through rows.
- The inner loop is used to iterate through columns.
Example:
public class TwoD_Traversal {
public static void main(String[] args) {
int[][] a= {{10 , 20, 30},
{11 , 22, 33},
{45 , 23 , 28 }};
System.out.println("*********************************");
System.out.println("Two Dimensional Array Traversal");
System.out.println("*********************************");
System.out.println();
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
System.out.print(a[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
}
Output:
*********************************
Two Dimensional Array Traversal
*********************************
10 20 30
11 22 33
45 23 28
Top comments (0)