Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Teaching Queues!
#1
A Queue is a collection class released in Java 5 as a design that hold elements and processes them in a particular order. Many implementations of Queue use an order were the elements which are submitted first, are processed first (First in, First out - FIFO). But its not limited to this order.

To start off, we are going to use a Queue to process three different numbers. In this example we are going to use ArrayQueue as the fastest and most straight-forward implementation of this Queue. No need going into advanced topics for anything this simple. With ArrayQueue you get a good FIFO order.

First we import the Queue interface,
Code:
import java.util.ArrayDeque;
import java.util.Queue;

Next we want to actually implement the Queue into our program,
Code:
Queue<E> aQueue = new ArrayDeque<E>();
Since E represents a specified element, we could use an object. Like so,
Code:
Queue<Object> aQueue = new ArrayDeque<Object>();

Now then. There are a small number of methods for use in the Queue interface, however they are very useful for controlling your Queue. Here is the API,
http://java.sun.com/j2se/1.5.0/docs/api/...Queue.html
As you can see, the majority of these methods include using the head of the Queue. The head of the Queue basically means the front, or the beginning.

Now lets get back to our Queue, first we implemented our Queue as an ArrayDeque and we can now start adding elements using offer(E) or add(). Its basically the same way you would add elements using an ArrayList or another Collection class. Like so,
Code:
taskQueue.add("y0");
or
Code:
taskQueue.offer("sup);
And to display the elements its as simple an an enchanced-for loop.

Here is an example program that adds elements to a Queue in FIFO order and displays them using the enchanced-for.
Code:
public class QueueExample {

    public static void main(String[] args) {
        Queue<Number> taskQueue = new ArrayDeque<Number>(); // accepts a Number object
            taskQueue.add(0); // we can use either add() or offer()
            taskQueue.offer(1); // to add elements to the queue
            taskQueue.offer(5.5);

        for (Number printQueue : taskQueue) // Number: printQueue, prints what is in our Queue
            System.out.println(printQueue);
    }

}

Dont know what object Number is? Just ask. ;)
As you can see, this can be VERY effective for many ideas. And for those RSPS programmers, this would be a good idea for a variety of mini-games.

Thanks for reading.
My SMF Modifications:
http://anthony.vibrantvps.com/smf
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Teaching Random Access Files! Project Evolution 0 783 02-27-2010, 10:35 PM
Last Post: Project Evolution
  Teaching Regular Expressions (Regex) Project Evolution 2 1,224 12-05-2009, 01:19 PM
Last Post: Project Evolution
  Teaching how to use the System Tray in Java! Project Evolution 0 743 11-07-2009, 04:28 PM
Last Post: Project Evolution

Forum Jump:


Users browsing this thread: 1 Guest(s)