Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

priorityQuery problem

 
varinder mahajan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops...His code has Smileys!!!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags (instead of quoting) when you post Java code.
 
varinder mahajan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help, i am unable to understand . where is the problem
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hope this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic