Creating an Array

From Progzoo
Jump to: navigation, search

You can declare an array, you can reserve space for the array, you can create an array literal.

Create an Array

You use square brackets to indicate that an array is required.

int [] x;
String [] s;

No memory has been set aside for these arrays yet. That is done with the new statement:

x = new int [10];
s = new String[20];

Notice that all elements of the x are initialised to 0 however the elements of s are null pointers - they do not cotain real strings.


[Font] [Default] [Show] [Resize] [History] [Profile]

Create an Array Literal

You can initialise the array to a specific list of values:


[Font] [Default] [Show] [Resize] [History] [Profile]

Create a two dimensional Array Literal

You can initialise the array to a specific list of values:


[Font] [Default] [Show] [Resize] [History] [Profile]

Create a String Array using split

The split method can be useful for creating string arrays:


[Font] [Default] [Show] [Resize] [History] [Profile]