Arrays

An array is a set of variables referenced by using a single variable name combined with an index number. Each item of an array is an element. All the elements in an array must be of the same type. Thus, the array itself has a type that specifies what kind of elements it can contain. An int array can contain int values, for example, and a String array can contain strings.

Written after the variable name, the index number is enclosed in brackets. So if the variable name is x, you could access a specific element with an expression like x[5].

tip.eps Index numbers start with 0 (zero) for the first element, so x[0] refers to the first element.

Declaring an array

Before you can create an array, you must declare a variable that refers to the array. This variable declaration should indicate the type of elements stored by the array, followed by a set of empty brackets, like this:

String[] names;

Here, a variable named names is declared. Its type is an array of String objects.

You can also put the brackets on the variable name rather than the type. The following two statements both create arrays of int elements:

int[] array1; // an array of int elements

int array2[]; // another array of int elements

Declaring an array doesn’t actually create the array. To do that, you must use the new keyword, followed by the array type. For example:

String[] names;

names = new String[10];

Or, more concisely:

String[] names = new String[10];

Initializing array elements

You can initialize an array by assigning values one by one, like this:

String[] days = new Array[7];

Days[0] = “Sunday”;

Days[1] = “Monday”;

Days[2] = “Tuesday”;

Days[3] = “Wednesday”;

Days[4] = “Thursday”;

Days[5] = “Friday”;

Days[6] = “Saturday”;

Or you can use the following shorthand:

String[] days = { “Sunday”, “Monday”, “Tuesday”,

“Wednesday”, “Thursday”,

“Friday”, “Saturday” };

Here, each element to be assigned to the array is listed in an array initializer. The number of values listed in the initializer determines the length of the array that the initializer creates.

Using loops with arrays

Frequently, arrays are processed within for loops. For example, here’s a for loop that creates an array of 100 random numbers, with values ranging from 1 to 100:

int[] numbers = new int[100];

for (int i = 0; i < 100; i++)

numbers[i] = (int)(Math.random() * 100) + 1;

Java also provides a special type of for loop called an enhanced for loop that’s designed to simplify loops that process arrays. An enhanced for loop allows you to skip the index variable, as in this example:

for (type identifier : array)

{

statements...

}

int[] numbers = new int[100];

for (int number : numbers

number = (int)(Math.random() * 100) + 1;

Using two-dimensional arrays

The elements of an array can be any type of object you want, including another array. This is called a two-dimensional array — or (sometimes) an array of arrays.

To declare a two-dimensional array, you simply list two sets of empty brackets, like this:

int numbers[][];

Here, numbers is a two-dimensional array of type int. To put it another way, numbers is an array of int arrays.

To create the array, you use the new keyword and provide lengths for each set of brackets, as in this example:

numbers = new int[10][10];

Here, the first dimension specifies that the numbers array has 10 elements. The second dimension specifies that each of those elements is itself an array with 10 elements.

To access the elements of a two-dimensional array, you use two indexes. For example:

int[5][7] = 23853;

Often, nested for loops are used to process the elements of a two-dimensional array, as in this example:

for (int x = 0; x < 10; x++)

{

for (int y = 0; y < 10; y++)

{

numbers[x][y] = (int)(Math.random() * 100) + 1

}

}

You can use an array initializer with a two-dimensional array, as in this example:

string members[][] =

{

{“Larry”, “Curly”, “Moe” },

{“Manny”, “Moe”, “Jack”},

{“Huey”, “Dewey”, “Louie”}

}

{25483.0, 22943.0, 38274.0, 33294.0}, // 2005

{24872.0, 23049.0, 39002.0, 36888.0}, // 2006

{28492.0, 23784.0, 42374.0, 39573.0}, // 2007

{31932.0, 23732.0, 42943.0, 41734.0} }; // 2008

When you create an array with an expression — such as new int[5][3] — you’re specifying that each element of the main array is actually an array of type int with three elements. Java, however, lets you create two-dimensional arrays in which the length of each element of the main array is different. Some­times, this is called a jagged array because the array doesn’t form a nice rectangle. Instead, its edges are jagged.

Arrays with more than two dimensions

Java doesn’t limit you to two-dimensional arrays. Arrays can be nested within arrays to as many levels as your program needs. To declare an array with more than two dimensions, you just specify as many sets of empty brackets as you need. For example:

int[][][] threeD = new int[3][3][3];

Here, a three-dimensional array is created, with each dimension having three elements. You can think of this array as a cube. Each element requires three indexes to access.

You can access an element in a multidimensional array by specifying as many indexes as the array needs. For example:

threeD[0][1][2] = 100;

This statement sets element 2 in column 1 of row 0 to 100.

You can nest initializers as deep as necessary, too. For example:

int[][][] threeD =

{ { {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} } };

Here, a three-dimensional array is initialized with the numbers 1 through 27.

You can also use multiple nested if statements to process an array with three or more dimensions. Here’s another way to initialize a three-dimensional array with the numbers 1 to 27:

int[][][] threeD2 = new int[3][3][3];

int value = 1;

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

for (int k = 0; k < 3; k++)

threeD2[i][j][k] = value++;

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset