• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Queue

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here when I comment the 2 lines the answer is 4 1. But pq contains 2 elements then why dosent it print 6???
Also if the other lines are uncommented I get ConcurrentModificationException, why am I not getting this excpetion when there are 2 elements in the queue??
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1) 1st time for loop:

for(String s:pq)
means:
if(pq.iterator().hasNext())
1) hasNext() checks cursor
2) here cursor = 0, and pq.size()=2 as pq=[4,6]
3) cursor<size 0<2 true

so:
s=(String)pq.next();
s=4;

pq.poll()
what pq.poll() does:
1) removes 4
2) does --size so size=1
3) cursor++ so cursor=1


2) 2nd time for loop:
for(String s:pq)
means:
if(pq.iterator().hasNext())
1) check cursor;<size ==> 1<1 false so will not go in second loop.

so output 41
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1) 1st time for loop:
for(String s:pq)
means:
if(pq.iterator().hasNext())
1) hasNext() checks cursor
2) here cursor = 0, and pq.size()=4 as pq[4,6,7,8]
3) cursor<size 0><4 true


Now
s=(String)pq.next();//here pq.next() checks
s=4;
what next() does:


Now pq.poll()
1) removes 4
2) does modCount++ so modCount=1
3) does --size so size =3
4) cursor++ so cursor=1


2) 2nd time for loop:

for(String s:pq)
means:
if(pq.iterator().hasNext())
1) hasNext() checks cursor
2) here cursor = 1, and pq.size()=3 as pq[6,7,8]
3) cursor<size 1><3 true


Now
s=(String)pq.next();//here pq.next() checks
what next() does:


 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which class are these defined ?
I mean what does these variable represent
expectedModCount != modCount
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Tharakan wrote:In which class are these defined ?
I mean what does these variable represent
expectedModCount != modCount



foreach loop internally uses Iterator of the collection, so here it is using inner iterator of the PriorityQueue class and that iterator is maintaining these counters.

And when we modify PriorityQueue using poll(), and we are modyfing PriorityQueue with PriorityQueue methods() and looping over it using inner iterator at that time these counter differs, as modCount is shared by both classes, but expectedModCount is only maintained by inner iterator class.

So poll() will increase modCount, but expectedModCount maintained by inner iterator class will remain unchanged, that's the way inner iterator find concurrent modification operation.

Try to see in source code of PriorityQueue.java, it will make everything crystal clear.
 
reply
    Bookmark Topic Watch Topic
  • New Topic