"Halo, Amigo."
"Hai, Risti."
"Ellie ngandhani yen sampeyan pengin luwih akeh conto koleksi. Aku bakal menehi sawetara. Iki dhaptar koleksi lan antarmuka: "
Antarmuka | Kelas/implementasi | Katrangan |
---|---|---|
Dhaptar | ArrayList | Dhaptar |
LinkedList | Dhaptar | |
Vektor | Vektor | |
tumpukan | tumpukan | |
Set | HashSet | Set |
PohonSet | Set | |
SortedSet | Diurutake set | |
peta | HashMap | Peta/kamus |
TreeMap | Peta/kamus | |
SortedMap | Kamus sing diurutake | |
Hashtable | Hash-meja |
"Hmm. Cukup akeh. Papat dhaptar, telung set, lan papat peta."
"Ya, kabeh iku implementasine beda saka dhaptar, Set lan antarmuka Peta."
"Apa bedane antarane implementasi iki?"
"Iki sing arep kita omongake dina iki. Sing sabar wae."
"Apa sampeyan duwe pitakonan liyane?"
"Aku ngerti carane nampilake dhaptar ing layar. Kepiye carane nampilake Set utawa Peta?"
"Elemen Dhaftar duwe urutan sing disetel, dadi sampeyan mung bisa nggunakake indeks kanggo nampilake. Kanggo Set utawa Peta, ora ana urutan tartamtu. Nyatane, urutan unsur-unsur kasebut bisa diganti nalika item dibusak utawa anyar. item ditambahake."
"Apik tenan."
"Iki sebabe obyek khusus, sing diarani iterator , diciptakake kanggo nggarap unsur koleksi. Dheweke ngidini sampeyan ngliwati kabeh unsur ing koleksi, sanajan mung duwe jeneng tinimbang indeks (Peta), utawa ora ana jeneng utawa indeks ( Setel).
"Iki sawetara conto:"
public static void main(String[] args)
{
Set<String> set = new HashSet<String>();
set.add("Rain");
set.add("In");
set.add("Spain");
// Get an iterator for the set
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) // Check if there is another element
{
// Get the current element and move to the next one
String text = iterator.next();
System.out.println(text);
}
}
public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
list.add("Rain");
list.add("In");
list.add("Spain");
Iterator<String> iterator = list.iterator();// Get an iterator for the list
while (iterator.hasNext()) // Check if there is another element
{
// Get the current element and move to the next one
String text = iterator.next();
System.out.println(text);
}
}
public static void main(String[] args)
{
// All elements are stored in pairs
Map<String, String> map = new HashMap<String, String>();
map.put("first", "Rain");
map.put("second", "In");
map.put("third", "Spain");
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext())
{
// Get a key-value pair
Map.Entry<String, String> pair = iterator.next();
String key = pair.getKey(); // Key
String value = pair.getValue(); // Value
System.out.println(key + ":" + value);
}
}
"Wah. Aku kepingin weruh apa tegese kabeh."
"Satemene cukup prasaja. Kaping pisanan, kita entuk obyek khusus, iterator, saka koleksi. Iterator mung duwe rong cara.
1. Sabanjure () cara ngasilake unsur sabanjuré ing koleksi.
2. Metode hasNext() mriksa apa isih ana unsur sing durung bali dening next().
"Oke. Aku iki wis saya cetha saiki. Ayo kula nyoba kanggo mbaleni maneh apa aku mangertos."
"Dadi ... Kaping pisanan, kita kudu nelpon metode iterator () ing koleksi kanggo entuk obyek iterator sihir iki."
"Banjur kita njaluk unsur siji-siji anggere ana kiwa kanggo njaluk. We njaluk unsur sabanjuré ing koleksi dening nelpon sabanjuré (), lan kita mriksa apa isih ana unsur ing koleksi dening nelpon hasNext () ing iterator. Apa bener?"
"Ya kurang luwih. Nanging ngenteni sing apik."
"Jawa nduweni notasi shorthand kanggo nggarap iterator. Nderek pola while and for , siji statement khusus wis ditambahake: kanggo saben . Iki uga dituduhake nggunakake tembung kunci kanggo ."
"Kanggo-saben statement mung digunakake kanggo nggarap koleksi lan kontaner. Iku nggunakake iterator implicitly, nanging kita mung ndeleng unsur bali."
"Ayo kula nuduhake sampeyan cara longhand lan shorthand kanggo nggarap iterator:"
public static void main(String[] args)
{
Set<String> set = new HashSet<String>();
set.add("Rain");
set.add("In");
set.add("Spain");
Iterator<String> iterator = set.iterator();
while (iterator.hasNext())
{
String text = iterator.next();
System.out.println(text);
}
}
public static void main(String[] args)
{
Set<String> set = new HashSet<String>();
set.add("Rain");
set.add("In");
set.add("Spain");
for (String text : set)
{
System.out.println(text);
}
}
"Elinga yen tembung sing disorot abang utawa ijo ora ana ing sisih tengen. Nyatane, telung baris diganti siji:"
Iterator<String> iterator = set.iterator();
while (iterator.hasNext())
{
String text = iterator.next();
for (String text : set)
"Iki katon apik banget. Aku luwih seneng karo cara iki."
"Ayo deleng versi singkatan saka conto ing ndhuwur:"
public static void main(String[] args)
{
Set<String> set = new HashSet<String>();
set.add("Rain");
set.add("In");
set.add("Spain");
for (String text : set)
{
System.out.println(text);
}
}
public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
list.add("Rain");
list.add("In");
list.add("Spain");
for (String text : list)
{
System.out.println(text);
}
}
public static void main(String[] args)
{
Map<String, String> map = new HashMap<String, String>();
map.put("first", "Rain");
map.put("second", "In");
map.put("third", "Spain");
for (Map.Entry<String, String> pair : map.entrySet())
{
String key = pair.getKey(); // Key
String value = pair.getValue(); // Value
System.out.println(key + ":" + value);
}
}
"Saiki sampeyan ngomong!"
"Aku bungah sing disenengi."
GO TO FULL VERSION