CodeGym /Blog Jawa /Acak /Queue poll () Metode ing Jawa karo Conto
John Squirrels
tingkat
San Francisco

# Queue poll () Metode ing Jawa karo Conto

## Apa iku Antrian?

Kaya jeneng kasebut, Antrian minangka struktur data umum ing Jawa miturut urutan sisipan First In First Out (FIFO) . Sampeyan bisa gampang mbayangno minangka antrian ing toko. Cepet sampeyan mlebu, luwih cepet sampeyan lunga. Tegese unsur sing ditambahake ing antrian sadurunge uga bakal lunga. Unsur pisanan (ngarep) antrian uga disebut sirah .
Fig 1.0: Antrian prasaja ing Jawa

## Apa poll() metode Antrian?

Jajak pendapat () cara ngijini sampeyan kanggo njupuk lan mbusak paling ndhuwur unsur (kepala) antrian a. Ing conto kita, nalika sampeyan nelpon poll () ing antrian ndhuwur, sampeyan bakal nampa '1' minangka asil. Lan mung ana 4 unsur sing isih ana ing antrian.
Fig 1.1: Sawise nggunakake poll () cara ing antrian

## poll() ing Jawa

Deleng ing conto prasaja saka nelpon poll () fungsi ing antrian ditampilake ing tokoh 1.0.
``````
import java.util.Queue;

public class QueuePollMethod {

public static void main(String[] args) {

// create a queue of die rolls

// Add 6 integers one by one

// print the original queue
System.out.println("Queue:\t" + dieRoll + "\n");

// after calling poll()
System.out.println("poll() returned : " + dieRoll.poll());
System.out.println("Queue Updated!\t" + dieRoll + "\n");

// after calling poll()
System.out.println("poll() returned : " + dieRoll.poll());
System.out.println("Queue Updated!\t" + dieRoll + "\n");

// after calling poll()
System.out.println("poll() returned : " + dieRoll.poll());
System.out.println("Queue Updated!\t" + dieRoll + "\n");
}
}
``````
Output
Antrian: [1, 2, 3, 4, 5, 6] poll() bali : 1 Antrian Dianyari! [2, 3, 4, 5, 6] poll () bali: 2 Antrian Dianyari! [3, 4, 5, 6] poll () bali: 3 Antrian Dianyari! [4, 5, 6]
Wigati dimangerteni, carane sawise saben jajak pendapat () ukuran telpon antrian suda 1 lan sirah bali.

Ayo goleki conto liyane dina ing seminggu. Kita bakal nelpon poll () siji-siji ing dina minggu lan ndeleng kaya apa.
``````
import java.util.Queue;

public class QueuePollMethod {

public static void main(String[] args) {

// print all the days in the week
System.out.println("Week Days: \t" + days + "\n");

// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");

// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");

// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");
}
}
``````
Output
Minggu Dina: [Minggu, Senin, Selasa, Rebo, Kamis, Jum'at, Setu] poll () bali: Minggu Dina Dianyari! [Senin, Selasa, Rebo, Kamis, Jum'at, Setu] poll () bali: Senin Dina Dianyari! [Selasa, Rebo, Kemis, Jum'at, Setu] poll () bali: Selasa Dina Dianyari! [Rebo, Kemis, Jumuah, Setu]

## Carane poll () beda saka Ndeleng () lan mbusak ()?

Gampang banget kanggo pamula nyampurake telung konsep sing beda amarga katon meh padha. Ayo "q" dadi antrian, banjur iki bedane:
• q.poll () : mbusak lan retrieves sirah antrian
• q.peek() : ora mbusak nanging mung njupuk / bali sirah antrian
• q.remove () : mbusak lan retrieves sirah antrian
Dadi katon sing mbusak () lan jajak pendapat () persis padha. Dheweke kerjane identik ing kahanan normal. Nanging nalika antrian kosong, mbusak () mbuwang NoSuchElementException , nalika polling ngasilake null .

Saiki ayo goleki sawetara conto saka telung fungsi ing ndhuwur.
``````
import java.util.Queue;

public class QueuePollMethod {

public static void main(String[] args) {

// print all the days in the week
System.out.println("Week Days: \t" + days + "\n");

// after calling peek()
System.out.println("peek() returned: " + days.peek());
System.out.println("Week Days: \t" + days + "\n");

System.out.println("peek() returned: " + days.peek());
System.out.println("Week Days: \t" + days + "\n");

// after calling remove()
System.out.println("remove() returned: " + days.remove());
System.out.println("Days Updated!\t" + days + "\n");

System.out.println("remove() returned: " + days.remove());
System.out.println("Days Updated!\t" + days + "\n");

System.out.println("remove() returned: " + days.remove());
System.out.println("remove() returned: " + days.remove());
System.out.println("remove() returned: " + days.remove());
System.out.println("remove() returned: " + days.remove());

System.out.println("Days Updated!\t" + days + "\n");

// after calling poll()
System.out.println("poll() returned: " + days.poll());
System.out.println("Days Updated!\t" + days + "\n");

System.out.println("poll() returned: " + days.poll());
System.out.println("remove() returned: " + days.remove());

}
}
``````
Output
Dina Minggu: [Minggu, Senin, Selasa, Rebo, Kamis, Jum'at, Setu] peek() bali: Minggu Minggu Dina: [Minggu, Senin, Selasa, Rebo, Kamis, Jum'at, Setu] peek() bali: Minggu Minggu Dina : [Minggu, Senin, Selasa, Rebo, Kamis, Jum'at, Setu] mbusak () bali: Minggu Dina Dianyari! [Senin, Selasa, Rebo, Kamis, Jum'at, Setu] mbusak () bali: Senin Dina Dianyari! [Selasa, Rebo, Kamis, Jum'at, Setu] mbusak () bali: Selasa mbusak () bali: Rebo mbusak () bali: Kamis mbusak () bali: Jumuah Dina Dianyari! [Saturday] poll () bali: Saturday Days Dianyari! [] jajak pendapat () bali: null Pangecualian ing thread "utama" java.util.NoSuchElementException ing java.util.LinkedList.removeFirst(LinkedList.java:270) ing java.util.LinkedList.remove(LinkedList.java:
Nalika sampeyan bisa ndeleng sawise nelpon Ndeleng () weekdays tetep padha. Lan nalika kita nggunakake mbusak () utawa jajak pendapat () ukuran antrian suda 1 bebarengan karo bali sirah. Menapa malih, java.util.NoSuchElementException dibuwang nalika kita nelpon mbusak () ing antrian kosong, nalika ora istiméwa kuwi di buwang kanggo nggunakake jajak pendapat () .

## Kesimpulan

Ing pungkasan, muga-muga sampeyan ngerti babagan panggunaan lan cara polling () saka Antrian. Nanging yen ana kebingungan, sampeyan bisa bali menyang kene kanggo konsultasi. Sugeng Coding!
Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION