Data Structures Demystified (Demystified)

The way to declare an array depends on the programming language used to write your program. In Java, there are two techniques for declaring an array. You can declare and initialize an array either where memory is allocated at compile time or where memory is dynamically allocated at runtime. Allocation is another way of saying reserving memory.

Let s begin by declaring an array where memory is reserved when you compile your program. This technique is similar in Java, C, and C++, except in Java you must initialize the array when the array is declared. There are four components of a statement that declares an array. These components are a data type, an array name , the total number of array element to create, and a semicolon (;). The semicolon tells the computer that the preceding is a statement. Here s the declaration statement in C and C++:

int grades[10];

In Java, you must initialize the array when the array is declared as shown here. The size of the array is automatically determined by counting the number of values within the braces. Therefore, there isn t any need to place the size of the array within the square brackets:

int[] grades = {0,0,0,0,0,0,0,0,0,0};

The data type is a keyword that tells the computer the amount of memory to reserve for each element of the array. In this example, the computer is told to reserve enough memory to store an integer for each array element.

The array name is the name you use within a program to reference an array element. The array name in this example is grades . The number within the square brackets is the total number of elements that will be in the array. The previous statements tell the computer to create an array of 10 elements.

Avoid making a common rookie mistake. Previously in this chapter you learned that the index for the first array element is zero, not one. Therefore, the tenth array element has the index value 9, not 10.

Some programs confuse an index with the total number of array elements. That is, they use the value 9 within the square brackets when declaring an array because they assume they are declaring 10 elements. In reality, they are declaring an array of 9 elements. The confusion stems from the fact that 9 is the index to reference the tenth array element.

With a little practice you can avoid making this mistake. Remember that the value within the square brackets in the statement that creates an array is not an index, although it resembles an index. This value is the number of array elements you need. That is, you insert the number 10 within the square brackets if you need 10 array elements. You use the index value of 9 if you want to access the tenth element.

In order to allocate memory at compile time, you must know the number of array elements that you need. Sometimes you don t know this, especially if your program loads the array with data stored in a database. The amount of data stored in a database typically fluctuates.

The solution in Java is to allocate memory at runtime. Programmers call this dynamically allocating memory . You dynamically allocate memory by using the new operator when declaring the array, as shown here:

int grades[] = new int[10];

This example looks a little strange , but it creates the same array as is created in the previous example. There are three things happening in this statement.

First, the new operator tells the computer to reserve 10 array elements, each the size of an int data type. The new operator returns a reference to the allocated memory.

Next, a reference to an int data type called grades is declared ( int grades[] ).

Last, the reference to the memory allocation returned by the new operator is assigned to the reference declared in the program.

This can be confusing even for experienced programmers to understand. If you re confused , remember this visitor s locker room example: a stadium has a locker room with Visitors on the door. This is similar to the reference grades[] . The visitor s locker room refers to the visiting team similar to the way grades[] refers to allocated memory: each game brings in a different visiting team who is assigned to the visitor s locker room. This is similar to assigning allocated memory to the reference grades[] .

Категории