• 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

priorityQuery problem

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops...His code has Smileys!!!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help, i am unable to understand . where is the problem
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hope this helps
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic