2. Topic
1. Primitive & Abstract Data Types
2. Lists, Stacks, and Queues
3. String & File
4. Trees
5. Balance Tree
6. Hashing
7. Heap
8. Sorting
9. Graph
10. Algoritma & Data Structure Analysis
3. Literature:
1. Data Structures and Algorithm Analysis in
Java, Mark A l l e n Weiss, Florida
International University 2012
2. Data Structures and Algorithms in Java Fifth
Edition International Student Version ,
Michael T. Goodrich, Department of
Computer Science University of California,
Irvine
5. Primitive & Abstract Data Types
Generally data types of all common language
divide by 2 types, they are:
• Primitive
• Abstract (Object)
6. Primitive data type
Primitive data type is type of data which have static
size of memory. Common this data types include:
• Integer : byte (8 byte), short (16 b), int (32 b),
long (64 b)
• Floating point: float (32 byte), double(64 b),
decimal(128 b), bigDecimal(256 b)
• Booleans: (1 bit)
• Characters: (1 byte)
• String: (n collection of character-size byte )
7. Primitive data type
Integer & double Data types, example:
30 byte of double used:1.073741824E9
30 byte of integer used:1073741824
32 byte of double used:4.294967296E9
32 byte of integer used:2147483647
64 byte of double used:1.8446744073709552E19
64 byte of integer used:2147483647
Proof: Maximum value of integer = 2^31 – 1
= 2147483647
8. Primitive data type
Integers & float Data types, example:
public static void main(String[] args) {
// TODO code application logic here
double d = Math.pow(2, 30);
int i = (int)d;
System.out.println("30 byte of integer used:"+i);
System.out.println("30 byte of integer used:"+i);
d = Math.pow(2, 32);
i = (int)d;
System.out.println("32 byte of integer used:"+i);
d = Math.pow(2, 64);
i = (int)d;
System.out.println("64 byte of integer used:"+i);
}
9. Primitive data type
Char & String Data Type, example:
char ch_a = 'A';
char ch_b = 'B';
System.out.println("Char: A("+ch_a+") Char B:"+ch_b);
String s = Character.toString(ch_a)+Character.toString(ch_b);
System.out.println("String S:"+s);
ch_a = s.charAt(0);
System.out.println("Char A:"+ch_a);
ch_b = s.charAt(1);
System.out.println("Char B:"+ch_b);
Proof : String is collection of characters
10. Dynamic Memory Create of
Primitive Data Type to Object
Base Type Class Name Creation Access
byte Byte n new Byte((byte}34); n.byteValueOf()
short Short n new Short((short}100); n.shortValueOf()
int Integer n = new Integer(1045); n.intValueOf()
long Long n = new Long(10849L); n.longValueOf()
float Float n = new Float(3.934F); n.floatValueOf()
double Double n new Double{3.934}; n.doubleValueOf()
11. Java Primitive to Dynamic
Object Casting
Usually, primitive data type is statically typing
for casting each others, but with using of
Object type can to cast dinamically.
Example:
int i = 100;
String s = String.valueOf(i);
System.out.println(s);
12. Java Primitive to Dynamic
Object Casting
Dynamic Object Casting,
Example:
public class TestMemory{
// Object for abstract data type
public Object read( ) {
return storedValue;
}
public void write( Object x ) {
storedValue = x;
}
13. Java Primitive to Dynamic
Object Casting
private Object storedValue;
public static void main(String[] args) {
TestMemory t = new TestMemory();
t.write( 33 );
String vi = (String) t.read().toString();
System.out.println( "Contents of String for integer are: " + vi );
TestMemory m = new TestMemory();
m.write( "my age: " );
String vs = (String) m.read();
System.out.println( "Contents of String are: " + vs + vi );
}
}
14. Collection of
Object Data Types
Collection is set of object with dynamic type
inside.
Type of collection in Java include:
• Array, static collection of the same primitive
data types with index.
• List, dynamic collection of objects with index
• Map, dynamic collection of objects with key &
value
15. Collection of
Array
public class Array{
int index = 10;
int [] arr= new int[index];
public void setArray(int index, int val){
for (int i=0;i<row;i++){
arr[i]= val;
}
}
public int getArray(int index){
return arr[index];
}
}
16. Collection of
Array
public class Matrik {
int bar = 10;
int kol = 10;
double [][] mtr = new double[bar][kol];
public void setMatrix(int row,int col, double val){
for (int i=0;i<row;i++){
for (int j=0;j<col;j++){
mtr[i][j]= val;
}
}
}
public double getMatrix(int row, int col){
return mtr[row][col];
}
}
17. Collection of
Dynamic Array
public void setValue(int row, int col, double value) {
if (row >= matrix.length) {
double[][] tmp = matrix;
matrix = new double[row + 1][];
System.arraycopy(tmp, 0, matrix, 0, tmp.length);
for (int i = row; i < row + 1; i++) {
matrix[i] = new double[col];
}
}
if (col >= matrix[row].length) {
double[] tmp = matrix[row];
matrix[row] = new double[col + 1];
System.arraycopy(tmp, 0, matrix[row], 0, tmp.length);
}
matrix[row][col] = value;
}
18. Collection of List
Object Data Types
import java.util.ArrayList;
import java.util.List;
public class Student {
String name;
String faculty;
int level;
static List listOfStudents = new ArrayList();
public String toString(){
return this.name+"|"+this.faculty+"|"+this.level;
}
Student(String pname, String pfaculty, int plevel){
this.name=pname;
this.faculty=pfaculty;
this.level=plevel;
}
19. Collection of List
Object Data Types
public static void main(String[] args) {
Student s = new Student("adi", "technic", 1);
s.listOfStudents.add(s);
System.out.println(s.toString());
s = new Student("wati", "economics", 3);
s.listOfStudents.add(s);
System.out.println(s.toString());
s.listOfStudents.add("gatot");
s.listOfStudents.add(1000);
for(int i=0; i<s.listOfStudents.size(); i++)
System.out.println(s.listOfStudents.get(i).toString());
}
}
20. Collection of Map
Object Data Types
public static void main(String[] args) {
HashMap<String, List> mapOfStudents = new HashMap<String, List>();
List<Student> ls = new ArrayList<Student>();
Student s = new Student("adi", "technic", 1);
ls.add(s);
s = new Student("eko", "technic", 3);
ls.add(s);
s = new Student("zaid", "technic", 5);
ls.add(s);
mapOfStudents.put("mahasiswa teknik", ls);
List result = mapOfStudents.get("mahasiswa teknik");
for(int i=0; i<result.size(); i++){
System.out.println(result.get(i).toString());
}
}
21. Exercise
Problem...?
class domain books
Publisher
- address: String
- city: String
- country: String
- email: email
- name: String
- state_province: String
- website: url
1
is published by
0..*
Author Book Chapter
are wrote by contains - abstract: String
- authId: String - authors: Author
- email: email - publication_date: Date 1 - book: Book
1..* 0..* 1..*
- first_name: String - publisher: Publisher - chapter: String
- last_name: String - title: String - content: String
22. Exercise
Quiz....!
If you have many class inside domain class like
picture above, how you can configure Java
Class to represent it?