GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/DataStructures/Queues/GenericArrayListQueue.java at master · classloader/Java Expand file tree/ Copy pathGenericArrayListQueue.javaMore file actions 83 lines (76 loc) · 2.3 KB / Copy pathGenericArrayListQueue.javaFile metadata and controls83 lines (76 loc) · 2.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 package DataStructures.Queues; import java.util.ArrayList; /** * This class implements a GenericArrayListQueue. * <p> * A GenericArrayListQueue data structure functions the same as any specific-typed queue. * The GenericArrayListQueue holds elemets of types to-be-specified at runtime. * The elements that are added first are the first to be removed (FIFO) * New elements are added to the back/rear of the queue. * */ public class GenericArrayListQueue<T> { /** * The generic ArrayList for the queue * T is the generic element */ ArrayList<T> _queue = new ArrayList<T>(); /** * Checks if the queue has elements (not empty) * * @return True if the queue has elements. False otherwise. */ private boolean hasElements() { return !_queue.isEmpty(); } /** * Checks what's at the front of the queue * * @return If queue is not empty, element at the front of the queue. Otherwise, null */ public T peek() { T result = null; if(this.hasElements()) { result = _queue.get(0); } return result; } /** * Inserts an element of type T to the queue. * * @param element of type T to be added * @return True if the element was added successfully */ public boolean add(T element) { return _queue.add(element); } /** * Retrieve what's at the front of the queue * * @return If queue is not empty, element retrieved. Otherwise, null */ public T poll() { T result = null; if(this.hasElements()) { result = _queue.remove(0); } return result; } /** * Main method * * @param args Command line arguments */ public static void main(String[] args) { GenericArrayListQueue<Integer> queue = new GenericArrayListQueue<Integer>(); System.out.println("Running..."); assert queue.peek() == null; assert queue.poll() == null; assert queue.add(1) == true; assert queue.peek() == 1; assert queue.add(2) == true; assert queue.peek() == 1; assert queue.poll() == 1; assert queue.peek() == 2; assert queue.poll() == 2; assert queue.peek() == null; assert queue.poll() == null; System.out.println("Finished."); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |