Topic
- Primitive & Abstract Data Types
- Lists, Stacks, and Queues
- String & File
- Trees
- Balance Tree
- Hashing
- Heap
- Sorting
- Graph
- Algoritma & Data Structure Analysis
Literature:
- Data Structures and Algorithm Analysis in Java, Mark A l l e n Weiss, Florida International University 2012
- Data Structures and Algorithms in Java Fifth Edition International Student Version , Michael T. Goodrich, Department of Computer Science University of California, Irvine
Primitive & Abstract Data Types
Generally data types of all common language divide by 2 types, they are:
- Primitive
- Abstract (Object)
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 )
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
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);
}
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
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()
|
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);
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;
}
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 );
}
}
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
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|
arr[i]= val;
}
}
public int getArray(int index){
return arr[index];
}
}
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|
for (int j=0;j
mtr[i][j]= val;
}
}
}
public double getMatrix(int row, int col){
return mtr[row][col];
}
}
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;
}
Collection of ListObject 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;
}
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
System.out.println(s.listOfStudents.get(i).toString());
}
}
Collection of MapObject Data Types
public static void main(String[] args) {
HashMap mapOfStudents = new HashMap();
List ls = new ArrayList();
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
System.out.println(result.get(i).toString());
}
}
ExerciseProblem...?
Download materi Disini
Kritik dan Saran layangkan ke Facebook dan Email: Hasan.kawaguchi24@gmail.com
Tidak ada komentar:
Posting Komentar