pTutorial

New way to learn Programming

Java string array example

This is a Java Program to create the Java string array and access the element of that array by using for loop and for each loop.

Java String Array Using for loop

class StringArray2 {
    public static void main(String args[]){
    String str[]=new String[3];
    str[0]="Java";
    str[1]="Java Array";
    str[2]="Java String";
        for(int i=0;i<str.length;i++){
            System.out.println(str[i]);
        }
    }
}

Output

Java
Java Array
Java String

Java String Array Using for loop

public class StringArray {
    public static void main(String args[]){
    String[] str={"java", "php","c/c++"};
        for(int i=0;i<str.length;i++){
            System.out.println(str[i]);
        }
    }
}

Output

java
php
c/c++

Java String Array Using for each loop

class StringArray1 {
    public static void main(String args[]){
    String[] str={"java", "php","c/c++"};
        for(String s:str){
            System.out.println(s);
        }
    }
}

Output

java
php
c/c++

See Also


QUOTES OF THE DAY

Knowledge itself is a power.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER