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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Return <strong>and</strong> update S to be the str<strong>in</strong>g obta<strong>in</strong>ed by <strong>in</strong>sert<strong>in</strong>g Q <strong>in</strong>side S<br />

start<strong>in</strong>g at <strong>in</strong>dex i.<br />

reverse():<br />

Reverse <strong>and</strong> return the str<strong>in</strong>g S.<br />

setCharAt(i,ch):<br />

Set the character at <strong>in</strong>dex i <strong>in</strong> S to be ch.<br />

charAt(i):<br />

Return the character at <strong>in</strong>dex i <strong>in</strong> S.<br />

Error conditions occur when the <strong>in</strong>dex i is out of the bounds of the <strong>in</strong>dices of the<br />

str<strong>in</strong>g. With the exception of the charAt method, most of the methods of the<br />

Str<strong>in</strong>g class are not immediately available to a Str<strong>in</strong>gBuffer object S <strong>in</strong><br />

<strong>Java</strong>. Fortunately, the <strong>Java</strong> Str<strong>in</strong>gBuffer class provides a toStr<strong>in</strong>g()<br />

method that returns a Str<strong>in</strong>g version of S, which can be used to access Str<strong>in</strong>g<br />

methods.<br />

Example 12.2: Consider the follow<strong>in</strong>g sequence of operations, which are<br />

performed on the mutable str<strong>in</strong>g that is <strong>in</strong>itially S = abcdefghijklmnop":<br />

Operation<br />

S<br />

append("qrs")<br />

"abcdefghijklmnopqrs"<br />

<strong>in</strong>sert(3,"xyz")<br />

"abcxyzdefghijklmnopqrs"<br />

reverse()<br />

"srqponmlkjihgfedzyxcba"<br />

setCharAt(7,'W')<br />

"srqponmWkjihgfedzyxcba"<br />

12.2 Pattern Match<strong>in</strong>g <strong>Algorithms</strong><br />

747

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

Saved successfully!

Ooh no, something went wrong!