This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

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: 9704
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: 9704
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
 
Dinner will be steamed monkey heads with a side of tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic