26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

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

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

316 Arrays Chapter 7<br />

Let us examine array c in Figure 7.1 more closely. The name of the array is c. Every<br />

array in <strong>Java</strong> knows its own length and maintains this information in a variable called<br />

length. The expression c.length accesses array c’s length variable <strong>to</strong> determine<br />

the length of the array. The array’s 12 elements are referred <strong>to</strong> as c[ 0 ], c[ 1 ], c[ 2 ],<br />

…, c[ 11 ]. The value of c[0] is -45, the value of c[ 1 ] is 6, the value of c[ 2 ] is<br />

0, the value of c[ 7 ] is 62 and the value of c[ 11 ] is 78. To calculate the sum of the<br />

values contained in the first three elements of array c and s<strong>to</strong>re the result in variable sum,<br />

we would write<br />

sum = c[ 0 ] + c[ 1 ] + c[ 2 ];<br />

To divide the value of the seventh element of array c by 2 and assign the result <strong>to</strong> the variable<br />

x, we would write<br />

x = c[ 6 ] / 2;<br />

Common <strong>Program</strong>ming Error 7.1<br />

It is important <strong>to</strong> note the difference between the “seventh element of the array” and “array<br />

element seven.” Because array subscripts begin at 0, the “seventh element of the array” has<br />

a subscript of 6, while “array element seven” has a subscript of 7 and is actually the eighth<br />

element of the array. This confusion is a source of “off-by-one” errors. 7.1<br />

The brackets used <strong>to</strong> enclose the subscript of an array are one of <strong>Java</strong>’s many opera<strong>to</strong>rs.<br />

Brackets are in the highest level of precedence in <strong>Java</strong>. The chart in Fig. 7.2 shows the precedence<br />

and associativity of the opera<strong>to</strong>rs introduced so far. They are shown <strong>to</strong>p <strong>to</strong> bot<strong>to</strong>m<br />

in decreasing order of precedence with their associativity and type. See Appendix C for the<br />

complete opera<strong>to</strong>r precedence chart.<br />

Opera<strong>to</strong>rs Associativity Type<br />

() [] . left <strong>to</strong> right highest<br />

++ -- right <strong>to</strong> left unary postfix<br />

++ -- + - ! (type) right <strong>to</strong> left unary<br />

* / % left <strong>to</strong> right multiplicative<br />

+ - left <strong>to</strong> right additive<br />

< >= left <strong>to</strong> right relational<br />

== != left <strong>to</strong> right equality<br />

& left <strong>to</strong> right boolean logical AND<br />

^ left <strong>to</strong> right boolean logical exclusive OR<br />

| left <strong>to</strong> right boolean logical inclusive OR<br />

&& left <strong>to</strong> right logical AND<br />

|| left <strong>to</strong> right logical OR<br />

?: right <strong>to</strong> left conditional<br />

= += -= *= /= %= right <strong>to</strong> left assignment<br />

Fig. Fig. 7.2 7.2 Precedence and associativity of the opera<strong>to</strong>rs discussed so far.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

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

Saved successfully!

Ooh no, something went wrong!