Contoh Program ArrayCollection
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListCollections {
public static void main(String[] args) {
String isine = "";
ArrayList<String> mbuh = new ArrayList<String>();
Scanner mlebu = new Scanner(System.in);
System.out.println("++++WELCOME TO MY PROGRAM++++");
while (true) {
try {
System.out.println("");
System.out.println("1. Tambah Data");
System.out.println("2. Lihat Data");
System.out.println("3. Hapus Data");
System.out.println("4. Edit Data");
System.out.println("5. Keluar");
System.out.print("Masukan pilihan : ");
int pilih = mlebu.nextInt();
switch (pilih) {
case 1:
System.out.println("\n++++TAMBAH DATA++++");
System.out.print(" Banyaknya Data : ");
int hamboh=mlebu.nextInt();
for (int k=0;k<hamboh;k++){
System.out.print(" Data String ke"+k+" : ");
isine=mlebu.next();
mbuh.add(isine);
}
break;
case 2:
System.out.println("\n++++PIU PIU PIU++++");
System.out.println("Jumlah Element arrayList: " + mbuh.size());
for (int i = 0; i < mbuh.size(); i++) {
System.out.println("Indeks ke " + i + ". " + mbuh.get(i));
}
System.out.println("");
break;
case 3:
int indeks;
System.out.println("\n++++HEMPASKAN++++");
try {
System.out.print("Masukan Index arraylist yang akan dibusak: ");
indeks = mlebu.nextInt();
mbuh.remove(indeks);
System.out.println("=-=-=-BUsak Data berhasil-=-=-=");
} catch (IndexOutOfBoundsException a) {
System.out.println("Indeks ArrayList Tumpeh Tumpeh");
}
break;
case 4:
int indekss;
String kata="";
System.out.println("\n++++APDET UPDET APDET++++");
try {
System.out.print("Masukan Index araylist yang akan diupdate: ");
indekss = mlebu.nextInt();
System.out.print("Masukan Data String araylist yang akan diupdate: ");
kata = mlebu.next();
mbuh.set(indekss,kata);
System.out.println("OOOOOOOwesss gantitOOOOOOOOOOO");
} catch (IndexOutOfBoundsException a) {
System.out.println("Indeks Array melebihi batas");
}
break;
case 5:
System.out.println("Gomawo ... Matur Nuhunn ");
System.exit(0);
default:
System.out.println("Inputan Tidak tersedia \n");
}
} catch (Exception e) {
System.err.println("Masukan Pilihan Kleru");
System.exit(0);
}
}
}
}
hasil program di jalankan :
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListCollections {
public static void main(String[] args) {
String isine = "";
ArrayList<String> mbuh = new ArrayList<String>();
Scanner mlebu = new Scanner(System.in);
System.out.println("++++WELCOME TO MY PROGRAM++++");
while (true) {
try {
System.out.println("");
System.out.println("1. Tambah Data");
System.out.println("2. Lihat Data");
System.out.println("3. Hapus Data");
System.out.println("4. Edit Data");
System.out.println("5. Keluar");
System.out.print("Masukan pilihan : ");
int pilih = mlebu.nextInt();
switch (pilih) {
case 1:
System.out.println("\n++++TAMBAH DATA++++");
System.out.print(" Banyaknya Data : ");
int hamboh=mlebu.nextInt();
for (int k=0;k<hamboh;k++){
System.out.print(" Data String ke"+k+" : ");
isine=mlebu.next();
mbuh.add(isine);
}
break;
case 2:
System.out.println("\n++++PIU PIU PIU++++");
System.out.println("Jumlah Element arrayList: " + mbuh.size());
for (int i = 0; i < mbuh.size(); i++) {
System.out.println("Indeks ke " + i + ". " + mbuh.get(i));
}
System.out.println("");
break;
case 3:
int indeks;
System.out.println("\n++++HEMPASKAN++++");
try {
System.out.print("Masukan Index arraylist yang akan dibusak: ");
indeks = mlebu.nextInt();
mbuh.remove(indeks);
System.out.println("=-=-=-BUsak Data berhasil-=-=-=");
} catch (IndexOutOfBoundsException a) {
System.out.println("Indeks ArrayList Tumpeh Tumpeh");
}
break;
case 4:
int indekss;
String kata="";
System.out.println("\n++++APDET UPDET APDET++++");
try {
System.out.print("Masukan Index araylist yang akan diupdate: ");
indekss = mlebu.nextInt();
System.out.print("Masukan Data String araylist yang akan diupdate: ");
kata = mlebu.next();
mbuh.set(indekss,kata);
System.out.println("OOOOOOOwesss gantitOOOOOOOOOOO");
} catch (IndexOutOfBoundsException a) {
System.out.println("Indeks Array melebihi batas");
}
break;
case 5:
System.out.println("Gomawo ... Matur Nuhunn ");
System.exit(0);
default:
System.out.println("Inputan Tidak tersedia \n");
}
} catch (Exception e) {
System.err.println("Masukan Pilihan Kleru");
System.exit(0);
}
}
}
}
hasil program di jalankan :