Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Collection Declare string array in Java (one and two dimensional)

Related Tutorials


 
 

Share on Google+Share on Google+

Declare string array in Java (one and two dimensional)

Advertisement
In this section we will learn how to declare string array in java. Java provides many ways to declare an array and initialize them for example- with 'new' keyword, without new keyword.

Declare string array in Java (one and two dimensional)

     

In this section we will learn how to declare string array in java. Java provides many ways to declare an array and initialize them for example- with 'new' keyword, without new keyword. Here we have given example to declare, initialize and access elements of one dimensional array and two dimensional as well.

1. String arr[] = new String[] {"mahendra", "anu", "girish", "komal"};
2. String arr[] = {"mahendra", "anu", "girish", "komal"};

Above code shows you how to declare the one dimensional array in Java.

Following is the program which explains you how to declare the array and then iterate through all the elements of the array.

ArrayDimensions.java

public class ArrayDimensions {
    public static void main(String[] args) {
        //declare and initialize one dimension array
        String[] names = new String[]{"mahendra", "anu", "girish", "komal"};
        System.out.println("These are elements of one Dim array.");
        for (int i = 0; i < names.length; i++) {
            System.err.println(names[i] + "   ");
        }
        //declare and initialize two dimensional array
        String dim2[][] = {
            {"mahendra", "girish"},
            {"sandeep", "vinnet"},
            {"amit", "komal"}
        };
        System.out.println("These are elements of two Dim array.");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.println(dim2[i][j]);
            }
        }
    }
}

Above program shows you how to declare single dimension array and iterate through all the elements.

Above program also teaches you how to declare two dimensional array in Java.

Advertisement

If you run the program you will get the following output:

Output :

These are elements of one Dim array.
mahendra   
anu   
girish   
komal   
These are elements of two Dim array.
mahendra
girish
sandeep
vinnet
amit
komal

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 1, 2008

Related Tutorials

Discuss: Declare string array in Java (one and two dimensional)  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
Shivkumar
June 9, 2013
Question

What the following statement describes . Please explain Class c= obj.getClass(); Constructor[] arr = c.getConstructors(); please send me answer on my email id: shivkumarmallesappa18@gmail.com
Cristina
November 11, 2011
create array based on input and uppercase function

Just passed this site.Thanks for the post.It's very easy to understand.I want to share my code.It's just a fragment of my seat reservation program and I'm still working on it.It accepts first input and loops for second input/s to be stored in single array created based on the first input while automatically converting it to uppercase. Works for input like combination of letters and numbers. Hope it helps! System.out.println("How many seats?: "); Scanner input = new Scanner(System.in); int num = input.nextInt(); System.out.println(); String[] seat= new String [num]; for(int x=0;x<num;x++) { int y = x + 1; System.out.println("Seat No." +y +": "); Scanner loc = new Scanner(System.in); seat[x] = loc.nextLine().toUpperCase();}
Steve Granger
April 17, 2012
Declare two-dimensional string array in Java

How would this code vary if I was declaring my two-dimensional array in the class containing "main" but needed to use it in a class called "Questions"?
rasida
June 26, 2012
java

how the two loops works in the above program.......
rasida
June 26, 2012
java

how the two loops works in the above program.......
Dave
December 2, 2012
Java two dimensional array

public static void main(String[] args){ String dim2[][] = { {"mahendra", " girish", " clear", " sure"}, {"sandeep", " vinnet", " dark", " not sure"}, {"amit", " komal", " horny", " great"}, {"adam", " bad", " will", " right"}, {"mahendra", " girish", " clear", " sure"} }; String output = ""; System.out.println("These are elements of two Dim array."); for (int i = 0; i < dim2.length; i++) { for (int j = 0; j < dim2.length - 1; j++) { output += dim2[i][j]; } output +="\n"; } System.out.println(output); } Prints : These are elements of two Dim array. mahendra girish clear sure sandeep vinnet dark not sure amit komal horny great adam bad will right mahendra girish clear sure
DMCA.com