19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

226 Chapter 6 Single-Dimensional Arrays<br />

myList[4] = 4.0;<br />

myList[5] = 34.33;<br />

myList[6] = 34.0;<br />

myList[7] = 45.45;<br />

myList[8] = 99.993;<br />

myList[9] = 11123;<br />

This array is illustrated in Figure 6.1.<br />

double[] myList = new double[10];<br />

myList<br />

Array reference<br />

variable<br />

reference<br />

Array element at<br />

index 5<br />

myList[0]<br />

myList[1]<br />

myList[2]<br />

myList[3]<br />

myList[4]<br />

myList[5]<br />

myList[6]<br />

myList[7]<br />

myList[8]<br />

myList[9]<br />

5.6<br />

4.5<br />

3.3<br />

13.2<br />

4.0<br />

34.33<br />

34.0<br />

45.45<br />

99.993<br />

11123<br />

Element value<br />

FIGURE 6.1 The array myList has ten elements of double type and int indices from 0 <strong>to</strong> 9.<br />

array vs. array variable<br />

array length<br />

default values<br />

0 based<br />

indexed variable<br />

Note<br />

An array variable that appears <strong>to</strong> hold an array actually contains a reference <strong>to</strong> that array.<br />

Strictly speaking, an array variable and an array are different, but most of the time the<br />

distinction can be ignored. Thus it is all right <strong>to</strong> say, for simplicity, that myList is an<br />

array, instead of stating, at greater length, that myList is a variable that contains a reference<br />

<strong>to</strong> an array of ten double elements.<br />

6.2.3 Array Size and Default Values<br />

When space for an array is allocated, the array size must be given, specifying the number of elements<br />

that can be s<strong>to</strong>red in it. The size of an array cannot be changed after the array is created.<br />

Size can be obtained using arrayRefVar.length. For example, myList.length is 10.<br />

When an array is created, its elements are assigned the default value of 0 for the numeric<br />

primitive data types, \u0000 for char types, and false for boolean types.<br />

6.2.4 Array Indexed Variables<br />

The array elements are accessed through the index. Array indices are 0 based; that is, they<br />

range from 0 <strong>to</strong> arrayRefVar.length-1. In the example in Figure 6.1, myList holds ten<br />

double values, and the indices are from 0 <strong>to</strong> 9.<br />

Each element in the array is represented using the following syntax, known as an indexed<br />

variable:<br />

arrayRefVar[index];<br />

For example, myList[9] represents the last element in the array myList.<br />

Caution<br />

Some programming languages use parentheses <strong>to</strong> reference an array element, as in<br />

myList(9), but <strong>Java</strong> uses brackets, as in myList[9].

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!