public class CreatingArrays
1: //CreatingArrays.java
2: //Illustrates various ways to create an array.
4: public class CreatingArrays
5: {
6: public static void main(String[] args)
7: {
8: //Here's one way to create an array of integers.
9: //Note that auto-boxing is in effect.
10: Integer[] integers1 = new Integer[]
11: {
12: 4, 1, 5, 3, 2
13: };
14: System.out.print("integers1: ");
15: for (Integer i : integers1)
16: {
17: System.out.print(i + " ");
18: }
19: System.out.println();
21: //Here's another (shorter) way (that creates the same array as above):
22: //And again, auto-boxing is in effect.
23: Integer[] integers2 =
24: {
25: 4, 1, 5, 3, 2
26: };
27: System.out.print("integers2: ");
28: for (int i : integers2)
29: {
30: System.out.print(i + " ");
31: }
32: System.out.println();
34: //=============================================================
35: //Now we repeat what we did in the above two code segments, but
36: //this time with strings instead of integers:
37: String strings1[] = new String[]
38: {
39: "klm", "abc", "xyz", "pqr"
40: };
41: System.out.print("strings1: ");
42: for (String s : strings1)
43: {
44: System.out.print(s + " ");
45: }
46: System.out.println();
48: String strings2[] =
49: {
50: "klm", "abc", "xyz", "pqr"
51: };
52: System.out.print("strings2: ");
53: for (String s : strings2)
54: {
55: System.out.print(s + " ");
56: }
57: System.out.println();
58: }
59: }
60: /* Output:
61: integers1: 4 1 5 3 2
62: integers2: 4 1 5 3 2
63: strings1: klm abc xyz pqr
64: strings2: klm abc xyz pqr
65: */