Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8897275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:17:52+00:00 2026-06-15T00:17:52+00:00

I have a queue in Java which is exhibiting strange behavior. If I call

  • 0

I have a queue in Java which is exhibiting strange behavior. If I call q.size(), After elements are pushed into the queue, only the first half of the items are popped out of the same queue. However, all of the items are popped if I store the value returned by q.size() before popping any items off of q. Why is that? The official documentation says:

public int size()

Returns the number of elements in this list.

Here is the code I am using. I used three different compilers: JDK 6.0_31, JDK 7.0_7, and Eclipse Compiler 0.A48. The results are the same.

import java.util.*;

public class StrangeQueueTest{
   public static void main(String[] args){
      Queue<String> q = new LinkedList<String>();
      String[] testData = {"1: one", "2: two", "3: three", "4: four", "5: five",
         "6: six", "7: seven", "8: eight", "9: nine", "10: ten", "11: eleven",
         "12: twelve", "13: thirteen", "14: fourteen", "15: fifteen",
         "16: sixteen", "17: seventeen", "18: eighteen", "19: nineteen",
         "20: twenty"};
      
      //Push items into the queue
      for(int x = 0; x < testData.length; x++)
         q.add(testData[x]);
      
      //Pop items out of the queue, calling size() at every iteration
      for(int x = 0; x < q.size(); x++)
         System.out.println(q.poll());
      
      //Pop items out of the queue, calling size() once
      /*int count = q.size();
      for(int x = 0; x < count; x++)
         System.out.println(q.poll());*/
   }
}

Why doesn’t the size() method work consitently? Am I doing something wrong? If so, what?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:17:53+00:00Added an answer on June 15, 2026 at 12:17 am

    You only get half the elements because you are counting from both ends and you stop when you reach the middle. Put another way: Every time you poll(), you reduce size() by one. At the same time, you are counting how many you have removed. The problem is that you have coded your loop to stop when the number you have removed is at least as large as the remaining size.

    Instead of this:

    for(int x = 0; x < q.size(); x++)
         System.out.println(q.poll());
    

    use this:

    while (q.size() > 0)
         System.out.println(q.poll());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java ActiveMQ producer which produces Integer messages into an ObjectMessage instance.
I have declared: queue<int, list<int> > Q After a series of calls: Q.push(37); Q.pop();
in my winforms app, I have a Queue which contains objects: Queue<MyObj> _queuedRows =
I have a server in Java which is multithreading, and I've created a thread
I have a java application where on thread is the socket connection which captures
I have a folder in which continuously new files are being dumped.In Java,what is
I am sending messages to a remote queue, to which I have no control.
I have a program whose only purpose is to drive a java.awt.Robot in an
I have an unbounded queue of jobs which can be processed asynchronously. The processing
I have about 5 different heterogenous linux dedicated server which can run Java applications.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.