I was asked in my exam to write an algorithm on queues in Java. I used an Iterator to extract the elements of the queue. (The elements consisted of chars : 'R' or 'W' or 'B' ONLY ). The question prohibited creating or destroying any character of the input queue. I just wanted to ask whether using Iterator to extract elements “creates” characters or not?
My algorithm was:
Q=Queue
Iterator dummy=Q.elements();
while (//some condition){
if (dummy.data=='R')
...
...
This is just the algorithm. I wanted to ask whether I’ve “created” elements or not because I was awarded 0 marks for it. 🙁
There is no method on queue like
elements()it iselement()whichRetrieves but does not remove element from the queuethe correct method isqueue.iterator();Any way logically you are some what correct and you didn’t deserve
0You can access using
new for loopalso