Adding and Accessing Queue Elements
by , 02-19-2012 at 07:05 AM (807 Views)
Call add() method for addition of the elements to Queue. This is an inherited type of method of Collection interface.
Different elements are added to a Queue which is explained by this given code.
In a Queue, added elements get stored in internal order and they are dependent upon implementation. Same happens with element retrieval.Java Code: This is the code to explain add operation in Java QueueQueue myQueue = new LinkedList(); myQueue.add("element 1"); myQueue.add("element 2"); myQueue.add("element 3");
To get 1st element from queue, use this code:
For removing or taking the 1st element, removal method is used. Following code explains it.Java Code: This is the code to get first element of Java QueueObject firstElement = myQueue.element();
Java Code: This is the code to remove elements from Java QueueQueue myQueue = new LinkedList(); myQueue.add("element 0"); myQueue.add("element 1"); myQueue.add("element 2"); //access via Iterator Iterator iterator = myQueue.iterator(); while(iterator.hasNext(){ String element = (String) iterator.next(); } //access via new for-loop for(Object object : myQueue) { String element = (String) object; }









Email Blog Entry
PDF to TIFF Conversion & Control...
Today, 11:39 AM in Java Software