Grouping Object Viska Mutiawani, M.Sc
Konsep Penting Array ArrayList Vector
Kenapa perlu mengelompokkan objek Banyak aplikasi melibatkan pengelompokan objek: Organizer. Notebook. Katalog buku. Jumlah data yang ingin disimpan bervariasi Ada item disimpan Ada item dihapus
Pengelompokan objek Ukuran tetap Array Ukuran berubah-ubah / fleksibel Paket Collection pada java.util ArrayList Vector LinkedList TreeSet HashMap
Array
Array Sintaks Tipe_data[] nama_variable = new tipe_data[ukuran]; Contoh: int[] tts = new int[100]; ATAU int[] tts; tts = new int[100]; ATAU int tts[]= new int[100];
Array (2) Memberi nilai kepada array: int[] nilai = {2, 3, 4, 5; char[] abjad= {a, b, c, d; Kita juga bisa membuat array tanpa nama: new int[]{2, 3, 5, 7, 11; menyediakan satu array baru dengan nilai-nilai tersebut. Sering dipakai untuk mengirim parameter ke method.
Array (3) Digunakan apabila kita ingin mengirimkan array sebagai parameter pada method. cetaklimaprima(new int[] {2,3,5,7,11 ); ATAU int[] bilprima = {2,3,5,7,11; cetaklimaprima(bilprima);
Multi-dimensi Array Data array disimpan di dalam indeks baris dan kolom int[][] arr=new int[3][3];//3 row and 3 column arr[0][0]=1; arr[0][1]=2; arr[0][2]=3; arr[1][0]=4; arr[1][1]=5; arr[1][2]=6; arr[2][0]=7; arr[2][1]=8; arr[2][2]=9;
Contoh class Testarray3{ public static void main(string args[]){ //declaring and initializing 2D array int arr[][]={{1,2,3,{2,4,5,{4,4,5; //printing 2D array for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ System.out.print(arr[i][j]+" "); System.out.println();
Array Sebagai Objek Apabila ia dianggap sebagai objek, maka ia mempunyai atribut dan method Atribut length Menentukan ukuran suatu array: nama_array.length int bilprima={2, 3, 5, 7, 11; for(int i=0; i<bilprima.length;i++) System.out.println(bilPrima[i]);
Method arraycopy() Mengcopy dari satu array ke array yang lain. Sintaksnya: System.arraycopy(sumber,indekSumber,sasaran, indeksasaran,bil); sumber :nama array yang hendak dicopy indeksumber :permulaan kedudukan array yang akan di copy dari sumber sasaran : array baru yang akan menyimpan kandungan array sumber indeksasaran :permulaan kedudukan pada array sasaran untuk menyimpan nilai yang dicopy bil : jumlah nilai yang dicopy
Metod arraycopy() int[] nomganjil ={1, 3, 5, 7, 9; int[] nompositif={10, 20, 30, 40, 50, 60; System.arraycopy(nomGanjil, 1, nompositif, 2, 3); for(int i=0; i<nompositif.length; i++){ Sysem.out.println( nompositif[ + i + ] selepas copy: +nompositif[i]); OUTPUT nompositif[0] selepas copy ialah 10 nompositif[1] selepas copy ialah 20 nompositif[2] selepas copy ialah 3 nompositif[3] selepas copy ialah 5 nompositif[4] selepas copy ialah 7 nompositif[5] selepas copy ialah 60
System.arraycopy(nomGanjil, 1, nompositif, 2, 3); Sebelum copy 1 10 3 20 5 30 7 40 9 50 60 Selepas copy 1 3 5 7 9 10 20 3 5 7 60
ArrayList
Notebook Catatan nota disimpan. Satu nota bisa ditampilkan. Tidak ada batasan berapa jumlah nota yang disimpan. Jumlah nota yang disimpan bisa diberitahukan.
Collection Tidak harus buat class collection sendiri, manfaatkan Java API. Ada paket java.util yang mengandung class dan interface yang berguna untuk pengelompokan objek. Beberapa class yang sering dipakai: ArrayList Vector
Struktur objek pada Notebook
Tambah nota baru
Index numbering
Fitur ArrayList Menambah kapasitas sesuai keperluan. Dapat menghitung jumlah data di dalamnya: size(). Setiap data tersimpan memiliki indeks. Menyusun objek sesuai urutan indeks. Data bisa ditambah dan dihapus. Nilai indeks bisa berubah jika data dihapus (atau ditambah). Method utama pada ArrayList adalah: add, get, remove dan size.
import java.util.arraylist; /** *... */ public class Notebook { // Storage for an arbitrary number of notes. private ArrayList notes; /** * Perform any initialization required for the * notebook. */ public Notebook() { notes = new ArrayList();...
Cara menggunakan ArrayList public class Notebook { private ArrayList notes;... public void storenote(string note) { notes.add(note); public int numberofnotes() { return notes.size(); Adding a new note Returning the number of notes (delegation)....
Retrieving an object Index validity checks public void shownote(int notenumber) { if(notenumber < 0) { // This is not a valid note number. else if(notenumber < numberofnotes()) { System.out.println(notes.get(noteNumber)); else { // This is not a valid note number. Retrieve and print the note
Hapus note berpengaruh pada order indeks
Iteration Dengan mengambil contoh Notebook, kita dapat melakukan suatu operasi yang berulang. Contoh: mencetak semua nota dalam Notebook. Untuk melakukan operasi berulang tersebut kita gunakan loop statements. Pada java ada 3 loop: for while do-while
While loop pseudo code General form of a while loop while keyword Boolean test while(loop condition) { loop body Statements to be repeated Pseudo-code example to print every note while(there is at least one more note to be printed) { show the next note
Implementasi pada Notebook /** * List all notes in the notebook. */ public void listnotes() { int index = 0; while(index < notes.size()) { System.out.println(notes.get(index)); index++; Increment by one
Cara lain dengan Iterator java.util.iterator Returns an Iterator object Iterator it = mycollection.iterator(); while(it.hasnext()) { call it.next() to get the next object do something with that object public void listnotes() { Iterator it = notes.iterator(); while(it.hasnext()) { System.out.println(it.next());
Vector
Class Vector Vector juga berupa array dinamis. Vector mirip seperti ArrayList, namun bedanya: Vector bersifat synchronized Vector memiliki method tambahan yang bukan bagian dari Java Collections Framework Vector dapat berguna untuk menyimpan data yang belum diketahui ukurannya. Atau tempat simpan yang dapat mengubah ukurannya sendiri.
Ada 4 constructor Vector() -> ukuran awal 10 Vector(int size) Vector(int size, int incr) Vector(Collection c)
Method List method pada Vector dapat dilihat di Java API. https://docs.oracle.com/javase/8/docs/api/
Coba ubah class Notebook sehingga menggunakan class Vector