0% found this document useful (0 votes)
92 views

Data Structures

The document discusses Java data structures and object-oriented programming concepts. It covers primitive data types like int and double, wrapper classes like Integer and Double, and commonly used classes like String, StringBuffer, Math, and Array. It also discusses container classes like List, Set, and Map and how to iterate over collections using iterators.

Uploaded by

Kien Do
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Data Structures

The document discusses Java data structures and object-oriented programming concepts. It covers primitive data types like int and double, wrapper classes like Integer and Double, and commonly used classes like String, StringBuffer, Math, and Array. It also discusses container classes like List, Set, and Map and how to iterate over collections using iterators.

Uploaded by

Kien Do
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Data structures

Object-Oriented Programming

Outline






Primitive data types


String
Math class
Array
Container classes
Readings:
 HFJ: Ch. 13, 6.
 GT: Ch. 13, 6.

i hc Cng ngh - HQG HN

Data structures

Basic classes
Object

Boolean

Character

Byte

i hc Cng ngh - HQG HN

Short

Void

Number

Integer

Math

Long

Data structures

String

StringBuffer ...

Float

Double

The Object class




Class getClass() returns class name of the current objects.


Cat a = new Cat(Tom);
Class c = a.getClass();
System.out.println(c);

boolean equals(Object) compares objects, is usually


redefined.
String toString() returns text representation of the object,
is usually redefined.

i hc Cng ngh - HQG HN

Data structures

Primitive data types




Utility methods:
 valueOf(String s) returns an object of the corresponding type
holding the value of String s.
Integer k = Integer.valueOf( "12"); // k = 12


typeValue() returns primitive value of the object


int i = k.intValue();

// i = 12

static parseType(String s) converts a string into a value of


the corresponding primitive type
int i = Integer.parseInt("12");

// i = 12

constants
 Type.MAX_VALUE, Type.MIN_VALUE

i hc Cng ngh - HQG HN

Data structures

The Character class




Methods
 static
 static
 static
 static
 static
 static
 static

boolean isUppercase(char ch)


boolean isLowercase(char ch)
boolean isDigit(char ch)
boolean isLetter(char ch)
boolean isLetterOrDigit(char ch)
char toUpperCase(char ch)
char toLowerCase(char ch)

i hc Cng ngh - HQG HN

Data structures

The String class





String: unmodifiable sequence of characters.


Initialise:
 String(String)
 String(StringBuffer)
 String(byte[])
 String(char[])
Methods
 int length the length of the string
 char charAt(int index) returns the character at position
index

i hc Cng ngh - HQG HN

Data structures

The String class




Comparison
 boolean equals(String)
 boolean equalsIgnoreCase(String)
 boolean startWith(String)
 boolean endWith(String)
 int compareTo(String)
Conversion
 String toUpperCase()
 String toLowerCase()
Concatenation
 String concat(String)
 operator +

i hc Cng ngh - HQG HN

Data structures

The String class




Search forwards
 int indexOf(int ch)
 int indexOf(int ch, int from)
 int indexOf(String)
 int indexOf(String s, int from)
Search backwards
 int lastIndexOf(int ch)
 int lastIndexOf(int ch, int from)
 int lastIndexOf(String)
 int lastIndexOf(String, int)

i hc Cng ngh - HQG HN

Data structures

The String class




Replace
 String replace(char oldChar, char newChar)
returns a new string resulting from replacing all
occurrences of oldChar with newChar
Substring
 String trim() returns a copy of the string, with leading
and trailing white space omitted.
 String substring(int startIndex)
 String substring(int start, int end)

i hc Cng ngh - HQG HN

Data structures

10

StringBuffer
StringBuffer: modifiable sequence of characters
 Initialize
 StringBuffer(String)
 StringBuffer(int length)
 StringBuffer(): default size is 16
 Utilities
 int length()
 void setLength()
 char charAt(int index)
 void setCharAt(int index, char ch)
 String toString()

i hc Cng ngh - HQG HN

Data structures

11

StringBuffer


Edit
 append(String)
 append(type t) appends t's string representation
 insert(int offset, String s)
 insert(int offset, char[] chs)
 insert(int offset, type t)
 delete(int start, int end) deletes a substring
 delete(int index) deletes 01 character
 reverse()

i hc Cng ngh - HQG HN

Data structures

12

The Math class




Constants
 Math.E
 Math.PI
Static methods
 type abs(type): absolute value of int/double/long
 double ceil(double)
 double floor(double)
 int round(float)
 long round(double)
 type max(type, type), type min(type, type)

i hc Cng ngh - HQG HN

Data structures

13

The Math class




Static methods (cont.)




double random() generates random value in the range [0.0,1.0]


double pow(double, double)

double exp(double) e raised to the power of a value.

double log(double) natural logarithm (base e)



double sqrt(double)
trigonometric


double sin(double) returns sine of an angle

double cos(double) returns cosine of an angle

double tan(double) returns tangent of an angle

i hc Cng ngh - HQG HN

Data structures

14

Array


An array is an object that


 contains a set of objects
 must be created (new) before use
int
a =
for
for

a[];
new int[10];
(int i = 0; i < a.length; i++) a[i] = i * i;
(int w: a)
System.out.print(w + " ");

int b[] = {2, 3, 5, 7};


a = b;
int m, n[];
double[] arr1, arr2;

i hc Cng ngh - HQG HN

Data structures

15

Array as argument and return value

int[] myCopy(int[] a)
{
int b[] = new int[a.length];
for (i=0; i<a.length; i++)
b[i] = a[i];
return b;
}
...
int a[] = {0, 1, 1, 2, 3, 5, 8};
int b[] = myCopy(a);

i hc Cng ngh - HQG HN

Data structures

16

Multi-dimensional arrays
int a[][];
a = new int[10][20];
a[2][3] = 10;
for (int i=0; i<a[0].length; i++)
a[0][i] = i;
for (int w: a[0])
System.out.print(w + " ");
int b[][] = { {1 , 2}, {3, 4} };
int c[][] = new int[2][];
c[0] = new int[5];
c[1] = new int[10];

i hc Cng ngh - HQG HN

Data structures

17

Array copy


System.arraycopy(src, s_off, des, d_off, len)


 src: source array, s_off: source array's offset
 des: destination array, d_off: destination array's offset
 len: number of entries to be copied
Entries's content is copied
 Primitive value
 Object reference.

i hc Cng ngh - HQG HN

Data structures

18

The Array class





In the package java.util


Four static methods
 fill() initialises array entries with one same value
 sort() sorts array





works with arrays of primitive values


works with classes that implement Comparable

equals() compares two arrays


binarySearch() performs binary search in sorted arrays,
creates logic error if used for unsorted arrays.

i hc Cng ngh - HQG HN

Data structures

19

Container classes
<< interface>>
Map

<< interface>>
Collection

HashMap

HashTable

<< interface>>
Set

<< interface>>
Sorted Map

<< interface>>
List
Tree Map

HashSet

<< interface>>
Sorted Set

Array List

Vector

LinkedList

TreeSet

i hc Cng ngh - HQG HN

Data structures

20

Iterator


An iterator allows a program to walk through a


collection and remove elements from the collection
during the iteration.
Java Iterator interface
 hasNext()
 next()
 remove()
Collection classes implement Iterator

i hc Cng ngh - HQG HN

Data structures

21

import java.util.*;
public class TestList {
static public void main(String args[])
{
Collection list = new LinkedList();
list.add(3);
list.add(2);
list.add(1);
list.add(0);
list.add("go!");

3
2
1
0
go!

Iterator i = list.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}
}

i hc Cng ngh - HQG HN

Data structures

22

import java.util.*;
public class TestList {
static public void main(String args[])
{
List list = new LinkedList();
list.add(3);
list.add(2);
list.add(1);
list.add(0);
list.add("go!");

3
2
1
0
go!

for (int i=0; i<list.size(); i++) {


System.out.println(list.get(i));
}
}
}

i hc Cng ngh - HQG HN

Data structures

23

import java.util.*;
public class TestList {
static public void main(String args[])
{
List<Integer> list = new LinkedList<Integer>();
list.add(3);
list.add(2);
list.add(1);
list.add(0);
//list.add("go!");

3
2
1
0

for (int i=0; i<list.size(); i++) {


System.out.println(list.get(i));
}
}
}

i hc Cng ngh - HQG HN

Data structures

24

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy