Search Java Programs

Wednesday, January 20, 2010

String Array Example

String Array example. This example shows you how to initialize String Array


This example shows you how to initialize String Array.

Here is the code

/**
* @ # StringArray.java
* A class repersenting how initialization String Arrays
* version 04 June 2008
* author Rose India
*/

public class StringArray {

public static void main(String args[]) {

//initialization String Arrays
String arr[] = {"Zero", "One", "Three"};

System.out.println("String Array arr ...");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}

String arr1[] = new String[]{"Zero", "One", "Two"};
System.out.println("\nString Array arr1...");
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
}

// This allocates the array but they are all null
String arr2[] = new String[3];

// here you start to stuff in the values.
arr2[0] = "Zero";
arr2[1] = "One";
arr2[2] = "Two";

System.out.println("\nString Array arr2...");
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
}
}
Output
String Array arr ...
Zero
One
Three

String Array arr1...
Zero
One
Two

String Array arr2...
Zero
One
Two

No comments:

Post a Comment

Website Design by Mayuri Multimedia