Monday, 7 May 2012

String Array Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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 
 * 
 */
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

Related Posts Plugin for WordPress, Blogger...