How to iterate an array in java?

 How to iterate an array in java?

Add Comment


  • 4 Answer(s)

    1) Using normal for loop

    2) Using extended new for loop

    Answered on February 19, 2019.
    Add Comment

    Iterating over Arrays in Java
    Iterating over an array means accessing each element of array one by one. There may be many ways of iterating over an array in Java, below are some simple ways.

    Method 1: Using for loop:
    This is the simplest of all where we just have to use a for loop where a counter variable accesses each element one by one.

    // Java program to iterate over an array
    // using for loop
    import java.io.*;
    class GFG {

    public static void main(String args[]) throws IOException
    {
    int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int i, x;

    // iterating over an array
    for (i = 0; i < ar.length; i++) {

    // accessing each element of array
    x = ar[i];
    System.out.print(x + " ");
    }
    }
    }
    Output :

    1 2 3 4 5 6 7 8
    Method 2: Using for each loop :
    For each loop optimizes the code, save typing and time.

    // JAVA program to iterate over an array
    // using for loop
    import java.io.*;
    class GFG {

    public static void main(String args[]) throws IOException
    {
    int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int x;

    // iterating over an array
    for (int i : ar) {

    // accessing each element of array
    x = i;
    System.out.print(x + " ");
    }
    }
    }
    Output :

    1 2 3 4 5 6 7 8

    Answered on February 19, 2019.
    Add Comment

    Java Program to show how to iterate over Array

    public class JavaForEachOverArray {
    public static void main(String args[]) {
    String[] languages = {"Java", "Scala", "C++", "Ruby", "Python", "Perl"};
    System.out.println("Iterating over String array using Java 1.5 foreach loop");
    for(String str : languages)
    {
                System.out.println(str);
     }
    System.out.println("Looping over String array using for loop");
     for(int i=0; i<languages.length/2; i++)
    { 
    System.out.println(languages[i]);
     }
    
    
    }
          
        }    
      
    Output:
     Iterating over String array using Java 1.5 foreach loop
     Java 
    Scala 
    C++ 
    Ruby
     Python
     Perl
     Looping over String array using for loop
     Java 
    Scala
     C++
    
    
    
    
        
    Answered on February 20, 2019.
    Add Comment

    or each loop optimizes the code, save typing and time.

    // JAVA program to iterate over an array
    // using for loop
    import java.io.*;
    class GFG {
     
        public static void main(String args[]) throws IOException
        {
            int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
            int x;
     
            // iterating over an array
            for (int i : ar) {
     
                // accessing each element of array
                x = i;
                System.out.print(x + " ");
            }
        }
    }

    Output :

    1 2 3 4 5 6 7 8

     

    Answered on February 21, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.