aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes priorityQuery problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "priorityQuery problem" Watch "priorityQuery problem" New topic
Author

priorityQuery problem

varinder mahajan
Ranch Hand

Joined: Jun 11, 2008
Posts: 47
import java.util.*;
class Demo
{
public static void main( String args[] )
{
PriorityQueue<Integer> p=new PriorityQueue<Integer>();
p.offer(1);
p.offer(1);
p.offer(1);
System.out.println("size:"+p.size());

for(int i )
{
System.out.println("poll");
p.poll();
System.out.println("size:"+p.size());
}
System.out.println("added two elements");
p.offer(1);
p.offer(1);
System.out.println("size:"+p.size());
for(int i )
{
System.out.println("peek");
p.peek();
System.out.println("size:"+p.size());
}
}
}



C:\>java Demo
size:3
poll
size:2
Exception in thread "main" java.util.ConcurrentModificationException

This is the output what is the problem
at java.util.PriorityQueue$Itr.next(Unknown Source)
at Demo.main(Demo.java:12)


Beat the world,if you can.......
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

You cannot modify the Collection with the enhanced for loop. Here you are removing elements (poll()) from the Queue within the enhanced for loop. Try using Iterator instead.
[ September 04, 2008: Message edited by: Vijitha Kumara ]

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Oops...His code has Smileys!!!


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Please use code tags (instead of quoting) when you post Java code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
varinder mahajan
Ranch Hand

Joined: Jun 11, 2008
Posts: 47
import java.util.*;
class Demo
{
public static void main( String args[] )
{
PriorityQueue<Integer> p=new PriorityQueue<Integer>();
p.offer(1);
p.offer(1);
p.offer(1);
System.out.println("size:"+p.size());

for(int i : p)
{
System.out.println("poll");
p.poll();
System.out.println("size:"+p.size());
}
System.out.println("added two elements");
p.offer(1);
p.offer(1);
System.out.println("size:"+p.size());
for(int i : p)
{
System.out.println("peek");
p.peek();
System.out.println("size:"+p.size());
}
}
}
varinder mahajan
Ranch Hand

Joined: Jun 11, 2008
Posts: 47
Please help, i am unable to understand . where is the problem
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

I read somewhere that if you add or delete any element from a queue, then any iterator created before the insertion or deletion is invalidated and calling any method on the iterator will cause ConcurrentModificationException
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775



Hope this helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: priorityQuery problem
 
Similar Threads
PriorityQueue doubt
Priority queue
Priority Queue
Generics
Collections..