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.

Example 12.1: Consider the follow<strong>in</strong>g set of operations, which are performed<br />

on the str<strong>in</strong>g S = "abcdefghijklmnop":<br />

Operation<br />

Output<br />

length()<br />

16<br />

charAt(5)<br />

'f'<br />

concat("qrs")<br />

"abcdefghijklmnopqrs"<br />

endsWith("javapop")<br />

false<br />

<strong>in</strong>dexOf("ghi")<br />

6<br />

startsWith("abcd")<br />

true<br />

substr<strong>in</strong>g(4,9)<br />

"efghij"<br />

With the exception of the <strong>in</strong>dexOf(Q) method, which we discuss <strong>in</strong> Section 12.2,<br />

all the methods above are easily implemented simply by represent<strong>in</strong>g the str<strong>in</strong>g as<br />

an array of characters, which is the st<strong>and</strong>ard Str<strong>in</strong>g implementation <strong>in</strong> <strong>Java</strong>.<br />

12.1.2 The <strong>Java</strong> Str<strong>in</strong>gBuffer Class<br />

The ma<strong>in</strong> methods of the <strong>Java</strong> Str<strong>in</strong>gBuffer class are listed below:<br />

append(Q):<br />

Return S+Q, replac<strong>in</strong>g S with S + Q.<br />

<strong>in</strong>sert(i, Q):<br />

746

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

Saved successfully!

Ooh no, something went wrong!