Hello,

I have got a Java queue and i want to print out the smallest number in the queue. Right now my program prints out the first number in the queue. Here is the code:

import java.util.ArrayList;
import java.util.Iterator;

public class Assign1 {
    private final int MAX_QUEUE_SIZE = 5;
    private final int NO_SIMULATIONS_TO_RUN = 7; 
    
    private int maxSize;

  private double[] queArray;

  private int nItems;

      /**
     * main method
     * @param args
     */
    public static void main( String args[] ) {
       new Assign1().runSimulation();
    }
    
    /**
     * run a simulation priority queue
     */
    public void runSimulation() {
        
        PriorityQueue queue = new PriorityQueue();
        initQueue(queue);
        
        for (int i = 0; i < NO_SIMULATIONS_TO_RUN; i++) {
            
            System.out.println( "The numbers in the queue are: " + queue.toString());
    
            System.out.println( "The current dispatched number is " + queue.poll());
            queue.remove();
            int item = getRandomNumber();
            
            System.out.println( "The new coming number is " + item );
            queue.add(item);   
            System.out.print( "\n\n" );
        }
    }
    /**
     * creates and inits a queue
     * @param queue
     */
    private void initQueue(PriorityQueue queue){
        for (int i = 0; i < MAX_QUEUE_SIZE; i++) 
            queue.add(getRandomNumber());
    }
    /**
     * returns a RandomNumber()
     * @return
     */
    public static int getRandomNumber(){
        return 1 + ( int ) ( Math.random() * 100 );
    }
    
    /**
     * custom queue class
     */
    private class PriorityQueue extends ArrayList {

        /**
         * allows us to add int values into an ArrayList
         * @param val
         */
        public void add(int val){
            super.add(new Integer(val));
        }
        /**
         * poll for the current top item in the list 
         * @return
         */
        public int poll(){
           return ((Integer) iterator().next()).intValue(); 
        }
        /**
         * returns the items in queue out
         * @param queue
         */
        public String toString(){
            StringBuffer buff = new StringBuffer();
            Iterator it = iterator();
            buff.append("[");
            while (it.hasNext()) {
                buff.append(it.next());
                if (it.hasNext())
                    buff.append(", ");
            }
            buff.append("]");
            return buff.toString();
        }
        /**
         * remove the top level item if the list is not empty
         */
        public void remove(){
            if (!isEmpty())
                remove(0);
        }
    }
}

thanks

Recommended Answers

All 4 Replies

Then remove or poll() the queue until you've looked at every element. Compare the smallest element found thus far to the current element, and set the smallest to the current if you find a smaller one.

i dont know much about queues. Do u have an idea of how i could start.

thanks

Read about queues on google, ask more specific questions, begin to write the code.

The standard library implementation of Priority queues is such that their elements are ordered based on the natural ordering of elements (in case of numbers, it would be in ascending order as per the contract of Comparable interface) or based on the external Comparator passed in. In that case, getting the smallest element would be a matter of removing the element present at the head of the queue.

Look into the implementation of PriorityQueue of the standard library for more details.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.