Data Structures
Data Structures
Object-Oriented Programming
Outline
Data structures
Basic classes
Object
Boolean
Character
Byte
Short
Void
Number
Integer
Math
Long
Data structures
String
StringBuffer ...
Float
Double
Data structures
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
// i = 12
// i = 12
constants
Type.MAX_VALUE, Type.MIN_VALUE
Data structures
Methods
static
static
static
static
static
static
static
Data structures
Data structures
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 +
Data structures
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)
Data structures
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)
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()
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()
Data structures
12
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)
Data structures
13
Data structures
14
Array
a[];
new int[10];
(int i = 0; i < a.length; i++) a[i] = i * i;
(int w: a)
System.out.print(w + " ");
Data structures
15
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);
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];
Data structures
17
Array copy
Data structures
18
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
Data structures
20
Iterator
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());
}
}
}
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!
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
Data structures
24