Docsity

Java - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Java - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Java APIs^ 

Think Java API (Application Programming Interface) as asuper dictionary of the Java language.

It has a list of all Java packages, classes, and interfaces; along withall of their methods, fields and constructors. ^

java.lang

intrinsic classes (

String

, etc)

^

java.io

reading and writing

^

java.util

Java Collection Framework and utility classes

^

javax.swing

GUI



Think Java API as the interface to manipulate Java classes asblack boxes

It tells you how to use Java classes but little how they areimplemented



Quick Demo

Strings and

String

Strings are an inevitable part of any programmingtask.



Printing messages (e.g. instant messaging with friends,output debugging information)



Representing data (e.g. student names )



Referring to files on disk (e.g. “c:\file.txt”)

Intuitively, think strings as a sequence of character

In Java

,String

has an array of

char

as its

internal representation.

String Properties^ 

String

is a class, not a primitive type.

It has fields, constructors, and methods.



Examples

Construct a string^ String

s

"Hello

world";

Get the length of the string^ int

length

s.length();//length

Access a part of the string^ String

sub

=^

s.substring(6);//"world"

char

c

=^

s.charAt(1);

//'e'

Concatenate strings^ String

t

"^

of

java";

String

s

s

+^

t;

//"Hello

world

of

java"

String Properties^ 

Q: What are the values of charArray1 and charArray1 at theend of the code

char[]

charArray

=^

{'a',

'b',

'c'};

char[]

charArray

=^

charArray1;

charArray1[1]

=^

'i';

//charArray

=^

{'a',

‘i',

'c'}

//charArray

=^

{'a',

‘i',

'c'}

String Properties^ Q: What are the values of s1 and s2 at the end of the code

String

s

"abc";

String

s

s

s

s

"def";

s

=^

"abcdef"

//s

"abc"

String Main Usage^ 

Check if two strings have the same prefix and postfix



Q: Does “hello world” starts with “hell”? String

s

"hello

world";

System.out.println(s.startsWith("hell"));//trueSystem.out.println(s.endsWith("world"));//true



Convert cases



Q: I want to turn “hello world” into upper cases. String

sUpper

=^

s.toUpperCase();//"HELLO

WORLD"

String Main Usage^ 

Finding the index of a substring



Q: I want to know the position of o’s in the string

String

s

=^

"Hello

world";

s.indexOf("o");//4, return -1 if the argument string not found

s.lastIndexOf("o");//

String Advanced Usage – breaking words apart

Q: I want to break "Hello world of java“ into "Hello”

“world” “of” “java“

String

s^

=^

"Hello

world

of

java";

StringTokenizer

st

new

StringTokenizer(s);

while

(st.hasMoreTokens())

System.out.println(st.nextToken()); }

//Hello//world//of//java

String Advanced Usage – breaking words apart

Q: I want to break "Hello, world of java“ into "Hello”

“world” “of” “java” (this is a comma in the string)

String

s^

=^

"Hello,

world

of

java";

StringTokenizer

st

new

StringTokenizer(s);

while

(st.hasMoreTokens())

System.out.println(st.nextToken()); }

//Hello,//world//of//java

String Advanced Usage – Concatenate strings repetitively^ 

Q: Suppose you have a list of 100 strings. You want toconcatenate them into one string?Solution 1^ List

stringList

=^

new

ArrayList();

for

(int

i

=^

i^

<^

i++)

stringList.add("string"

+^

i);

} String

finalString

for

(Iterator

iter

stringList.iterator();

iter.hasNext();

)^

String

s

=^

(String)

iter.next();

finalString

s;

//work

but

inefficient

for

large

strings

and

large

amount

of

concatenation

because

String

is

immutable.

Intuitively,

Java

needs

to

maintain

an

a^

old

string,

a

new

string,

and

a

concatenate

string

in

each

iteration.

String Advanced Usage – Concatenate strings repetitively^ 

Q: Suppose you have a list of 100 strings. You want toconcatenate them into one string?Solution 2^ List

stringList

=^

new

ArrayList();

for

(int

i

=^

i^

<^

i++)

stringList.add("string"

+^

i);

} StringBuffer

sb

=^

new

StringBuffer();

//StringBuffer

is

mutable.

Recommended

for

a

large

amount

of

string

concatenation.

for

(Iterator

iter

stringList.iterator();

iter.hasNext();

String

s

=^

(String)

iter.next();

sb.append(s); }

String Advanced Usage – String and OOP



Q: I have just written a class. How can I represent theclass with a string?



Overwrite toString()

public

class

Point

double

x;

double

y;

Point(double

x,

double

y)

this.x

x;

this.y

y;

public

String

toString()

return

"x

+^

x^

y

"^

+^

y;

} Point

p^

new

Point(2,3);

System.out.println(p);

x = 2.0, y = 3.

String Advanced Usage – Convert between strings toprimitives

Q: I want to convert an integer 123 to a string and convert astring “123” to an integer



Use

String.valueOf(), Integer.parseInt()

int

i^

=^

String

s^

=^

i

String

t^

=^

String.valueOf(i);

String

u^

=^

Integer.toString(i);

//s

=^

t^

u

=^

int

j^

=^

Integer.parseInt(s);

j

//Extension

–^

Double.parseDouble();