/** * Implements PriorityQueue1 using an unsorted ArrayList */ import java.util.*; public class PQ1ArrayList> { private ArrayList list; public PQ1ArrayList() { this.list = new ArrayList(); } /** * Returns the number of items in the queue. */ public int size() { return list.size(); } /** * Adds a new item to the priority queue. * @param item the item to add */ public void add(T item) { this.list.add( item ); } /** * Removes the item with highest priority from the queue. * @return */ public T remove() { if (size() == 0) return null; T biggestSoFar = list.get(0); for (T item: list) { if (biggestSoFar.compareTo( item ) < 0) { biggestSoFar = item; } } this.list.remove( biggestSoFar ); return biggestSoFar; } public static void main( String[] args ) { PQ1ArrayList pq = new PQ1ArrayList(); pq.add( 5 ); pq.add( 10 ); pq.add( 1 ); System.out.println( "removing " + pq.remove() ); System.out.println( "size is now " + pq.size() ); } }