0% menganggap dokumen ini bermanfaat (0 suara)
35 tayangan21 halaman

Sda 14 ArrayList

Diunggah oleh

Tarbiyatun Navi
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PPTX, PDF, TXT atau baca online di Scribd
0% menganggap dokumen ini bermanfaat (0 suara)
35 tayangan21 halaman

Sda 14 ArrayList

Diunggah oleh

Tarbiyatun Navi
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PPTX, PDF, TXT atau baca online di Scribd

UNIVERSITAS

YA R S I

ArrayList

Struktur Data dan Algoritma

CC BY-NO-SA
4.0 International

Content by:
Andreas F., Herika H., M. Faturrachman, and Nova E.D.
Image(s):
[Link] Desain by Arksnet – Desain Modified by Andreas F.
BY-NO-SA UNIVERSITAS
CC
4.0 International YA R S I
Lisensi Distribusi
Materi, urutan penyajian, animasi, dan pertanyaan yang ada dalam
berkas ini merupakan karya orang-orang yang namanya disebutkan
di halaman pertama, dan sengaja didistribusikan dengan lisensi
Creative Common by-no-sa 4.0 International, yang berarti bisa
digunakan dan diubah sesuai dengan kebutuhan selama tidak untuk
kepentingan komersial dan memberikan kredit pada pencipta. Karya
ini dan ubahannya boleh didistribusikan ulang selama masih
menggunakan lisensi yang sama. Gambar yang digunakan
merupakan milik penciptanya masing-masing.

Informasi lebih lanjut silahkan lihat:


[Link]

2
UNIVERSITAS
YA R S I

ArrayList

3
UNIVERSITAS
YA R S I ArrayList

ArrayList merupakan sebuah abstraksi Array


dinamis yang disediakan Java™ dalam package
[Link]. Kelas ini mempermudah banyak
manipulasi Array dengan mengorbankan
kecepatan mengakses dan memproses data.

Image by @[Link] 4
UNIVERSITAS
YA R S I Import ArrayList

Pastikan Anda meng-import kelas ArrayList sebelum


menggunakannya:

import [Link]; Wajib!

public class Tes{


    public static void main(String[] args) {
        
    }
}

5
UNIVERSITAS
YA R S I Membuat ArrayList

Deklarasi dan pembuatan ArrayList mirip dengan


mendeklarasikan dan membuat String atau Scanner, yaitu:

tipe data identifier objek/value


ArrayList nama = new ArrayList();

sama atau kompatibel

6
UNIVERSITAS
YA R S I Soal 1

Identifikasi setiap variabel dan tipe data dari potongan kode


program berikut:

ArrayList daftarBarang = new ArrayList();


int jumlahBarangPribadi = 25;
String[] daftarBarangPribadi =
new String[jumlahBarangPribadi];
daftarBarangPribadi[0] = "Bose Sound Bar";
[Link](daftarBarangPribadi[0]);
daftarBarangPribadi[1] = "Tas Ransel";
[Link](daftarBarangPribadi[1]);
7
UNIVERSITAS
YA R S I Prilaku ArrayList

String tomato = "Tomato";


ArrayList fruit = ArrayList(); tomato Tomato

[Link]("Apple");
fruit Apple 0
[Link](tomato);
Tomato
Mango 1
[Link]("Garlic");
Mango
Durian
Garlic 2
[Link](2, "Mango");
Cucumber 3
[Link](tomato);
[Link]("Durian");
[Link]("Cucumber");
[Link](3);
[Link](fruit);
Output:

[Apel, Mango, Durian]

8
UNIVERSITAS
YA R S I Soal 2

Apakah keluaran dari potongan program berikut:


[Link] kuliah = new [Link]();
[Link]("DDP");
[Link]("Matdis 1");
[Link]("PTI");

[Link](kuliah);

9
UNIVERSITAS
YA R S I Method ArrayList

Modifier and Type Method and Description


boolean add(E e)
Appends the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element at the specified position in this list.
E get(int index)
Returns the element at the specified position in this list.
E remove(int index)
Removes the element at the specified position in this list.
boolean remove(Object o)
Removes the first occurrence of the specified element from this
list, if it is present.
E set(int index, E element)
Replaces the element at the specified position in this list with the
specified element.
int size()
Returns the number of elements in this list.

[Link]
10
UNIVERSITAS
YA R S I Soal 3

Buatlah sebuah ArrayList dan kemudian lakukan langkah-


langkah berikut:
1. Simpan 100.
2. Simpan 200.
3. Sisipkan 150 di antara 100 dan 200.
4. Simpan 300.
5. Hapus data pada indeks pertama.
6. Cetak data pertama dan terakhir.

11
UNIVERSITAS
YA R S I

Pemeriksaan Tipe Data


dalam ArrayList

12
UNIVERSITAS
YA R S I Soal 4

Jelaskan ke anehan yang ada pada potongan kode program


berikut:
[Link] list =
new [Link]();

[Link]("DDP");
[Link](' ');
[Link](2019);
[Link](true);

13
UNIVERSITAS
YA R S I E-Type

Modifier and Type Method and Description


boolean add(E e)
Appends the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element at the specified position in this list.

E element:
Semua tipe data reference!

14
UNIVERSITAS
YA R S I Tipe Data Primitif?

Jika ArrayList hanya bisa menyimpan tipe


data Reference, kenapa ia bisa
menyimpan tipe data primitif (e.g., int)?

Image by @[Link] 15
UNIVERSITAS
YA R S I Wrapper Classes

Ketika disimpan ke dalam ArrayList, seluruh tipe data primitif


secara otomatis diubah menjadi tipe data Reference dengan
memanfaatkan kelas wrapper.
[Link](2000);
[Link](121.212);
[Link](false);

[Link]([Link](2000));
[Link]([Link](121.212));
[Link]([Link](false));

Image by @[Link] 16
UNIVERSITAS
YA R S I Soal 5

Potensi masalah apa yang bisa muncul dengan mengizinkan


ArrayList untuk menyimpan berbagai macam tipe data
Reference?

17
UNIVERSITAS
YA R S I Compilation Note

ArrayList list = new ArrayList();


[Link]("someone");

>> javac [Link]


Note: [Link] uses or unchecked or unsafe operations.
Note: Recompile with -Xlint: unchecked for details.

18
UNIVERSITAS
YA R S I Xlint Note

ArrayList list = new ArrayList();


[Link]("someone");

>> javac –Xlint [Link]


ArrayList is a raw type. References to generic type
ArrayList<E> should be parameterized.
Type safety: The method add(Object) belongs to the raw
type ArrayList. References to generic type ArrayList<E>
should be parameterized

19
UNIVERSITAS
YA R S I Generic

Semua elemen dalam ArrayList bertipe String!


ArrayList<String> list = new ArrayList<String>();
[Link]("someone");

>> javac [Link]

>>

20
UNIVERSITAS
YA R S I Soal 6

Deklarasikan sebuah ArrayList yang hanya akan menyimpan


data dengan tipe Array of Array of Boolean (i.e., Array
multidimensi)!

21

Anda mungkin juga menyukai