Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Thursday, November 9, 2017

Iterating String Array Examples in Java - Iterate Through String Array in Java

For-Each Example: Enhanced for Loop to Iterate Java Array:

How to iterate String array in Java. This is a common scenario which is being used in our projects.
Let us take a string array example that we want to iterate over it.

Note: Array index always starts from 0 programmatically.


String Array Iterating:

A String Array is a data structure that holds several String values. The number of String elements is fixed and can't be changed. 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. 

We can do this in mainly three ways.

Step 1): Using for loop
Step 2): Using while loop
Step 3): Using For-Each loop


Iterating String Array Examples in Java



1) Iterate String array using For loop:

For Loop, Nested for loop in Java

For is a basic loop to loop anything which stores collection of values or to execute a set of statements for a defined number of times.

package com.java.w3schools.string.array;

// java-w3schools blog
public class StringArrayIterateUsingFor {
 public static void main(String[] args) {

  String[] fruits = { "Apple", "Orange", "Kiwi" };
  int length = fruits.length;

  System.out.println("Iterating Fruit names using for loop");
  for (int i = 0; i < length; i++) {
   System.out.println(fruits[i]);
  }
 }
}

Output:


Iterating Fruit names using for loop
Apple
Orange
Kiwi

Used for loop through string array from index 0 to its length. variable i represents index of array and loop three times because array has three values in it. Prints Apple, Orange, Kiwi as above.


2) Iterate String array using While loop:

While loop in Java



package com.java.w3schools.string.array;

// java-w3schools blog
public class StringArrayIterateUsingWhile {
    public static void main(String[] args) {
        String[] fruits = { "Apple", "Orange", "Kiwi" };
        int length = fruits.length;

        System.out.println("Iterating Fruit names using while loop");
        int i = 0;
        while (i < length) {
            System.out.println(fruits[i]);
            i++;
        }
    }
}


Output:



Iterating Fruit names using while loop
Apple
Orange
Kiwi

Iterating using while loop is same as for loop but we have to initialize the index variable outside while loop.
For loop is preferable and easy to use, flexible than while loop iterating arrays.


3) Iterate String array using For-Each loop:

Drawback of for loop and For-Each loop in java

This is advent of new java version from 1.5 onward which is very flexible and reduces the amount time for writing code for iteration.
This is suitable for arrays and collections. But we should know the type of the array.


package com.java.w3schools.string.array;
// java-w3schools blog
public class StringArrayIterateUsingForEach {
    public static void main(String[] args) {
        String[] fruits = { "Apple", "Orange", "Kiwi" };
        int length = fruits.length;

        System.out.println("Iterating Fruit names using for each loop");
        int i = 0;
        for (String fruitName : fruits) {
            System.out.println(fruitName);
        }
    }
}

Output:



Iterating Fruit names using for each loop
Apple
Orange
Kiwi

The main drawback of for each is we can not start looping from index 3 to its lenght. It always from first value to last value which is not suitable in some cases. In these types of cases, for and while loops are best suitable.


How to get the current and next value using for loop:


package com.java.w3schools.string.array;

// java-w3schools blog
public class ArrayCurrentNext {
    public static void main(String[] args) {

        String[] countryCodes = { "India", "IN", "America", "USA", "Australia", "AUS" };
        int length = countryCodes.length;

        System.out.println("Getting Current and next value : ");
        for (int i = 1; i < length; i += 2) {
            System.out.println("Country Name: " + countryCodes[i - 1] + ", Code: " + countryCodes[i]);
        }
    }
}


Output:



Getting Current and next value : 
Country Name: India, Code: IN
Country Name: America, Code: USA
Country Name: Australia, Code: AUS


Summary:

Now, You are aware how to iterate in different ways using for, while, for each loop. We wisely use the proper case based on use-case.

Please leave your questions and feedback in comments.

No comments:

Post a Comment

Please do not add any spam links in the comments section.