23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

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.

the array, we will sometimes refer to the length of an array as its capacity. We show<br />

another simple use of an array <strong>in</strong> the follow<strong>in</strong>g code fragment, which counts the<br />

number of times a certa<strong>in</strong> number appears <strong>in</strong> an array:<br />

/** Counts the number of times an <strong>in</strong>teger appears <strong>in</strong><br />

an array. */<br />

public static <strong>in</strong>t f<strong>in</strong>dCount(<strong>in</strong>t[] a, <strong>in</strong>t k) {<br />

<strong>in</strong>t count = 0;<br />

for (<strong>in</strong>t e: a) {<br />

// note the use of the "foreach" loop<br />

}<br />

if (e == k) // check if the current element equals k<br />

count++;<br />

return count;<br />

}<br />

Out of Bounds Errors<br />

It is a dangerous mistake to attempt to <strong>in</strong>dex <strong>in</strong>to an array a us<strong>in</strong>g a number outside<br />

the range from 0 to a.length &m<strong>in</strong>us; 1. Such a reference is said to be out of<br />

bounds. Out of bounds references have been exploited numerous times by hackers<br />

us<strong>in</strong>g a method called the buffer overflow attack to compromise the security of<br />

computer systems written <strong>in</strong> languages other than <strong>Java</strong>. As a safety feature, array<br />

<strong>in</strong>dices are always checked <strong>in</strong> <strong>Java</strong> to see if they are ever out of bounds. If an array<br />

<strong>in</strong>dex is out of bounds, the run-time <strong>Java</strong> environment signals an error condition.<br />

The name of this condition is the ArrayIndexOutOfBoundsException. This<br />

check helps <strong>Java</strong> avoid a number of security problems (<strong>in</strong>clud<strong>in</strong>g buffer overflow<br />

attacks) that other languages must cope with.<br />

We can avoid out-of-bounds errors by mak<strong>in</strong>g sure that we alway <strong>in</strong>dex <strong>in</strong>to an<br />

array, a, us<strong>in</strong>g an <strong>in</strong>teger value between 0 <strong>and</strong> a.length. One shorth<strong>and</strong> way we<br />

can do this is by carefully us<strong>in</strong>g the early term<strong>in</strong>ation feature of Boolean operations<br />

<strong>in</strong> <strong>Java</strong>. For example, a statement like the follow<strong>in</strong>g will never generate an <strong>in</strong>dex<br />

out-of-bounds error:<br />

if ((i >= 0) && (i < a.length) && (a[i] > 2) )<br />

× = a[i];<br />

61

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

Saved successfully!

Ooh no, something went wrong!