Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-28-2008, 02:19 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Stack not popping
The code below should pop 4 elements, but it just shows 2 elements. I am not able to understand the prolem there:
Code:
Stack stack = new Stack(); stack.push("Obj1"); stack.push("Obj2"); stack.push("Obj3"); stack.push("Obj4"); System.out.println(stack.size()); for(int i=0;i<stack.size();i++) { System.out.println("Stack header is: " + stack.peek()); System.out.println("Popped value is: " + stack.pop()); }
Output:

Code:
Stack header is: Obj4 Popped value is: Obj4 Stack header is: Obj3 Popped value is: Obj3
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-28-2008, 04:28 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
Your problem is caused by

Code:
for(int i=0;i<stack.size();i++)
you are getting the size of the stack each time round but your index is increasing in value while the stack size is decreasing so:

loop 1, i = 0, stack.size() = 4, you print values
loop 2, i = 1, stack.size() = 3, you print values
loop 3, i = 2, stack.size() = 2, and you drop out of the loop

take the original stack size first and then use that for the loop check, i.e.

Code:
int original_stack_size = stack.size(); for(int i=0; i< original_stack_size; i++)
as well as fixing the problem it is also more efficient
__________________
-- Hope that helps
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-28-2008, 05:59 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Thanks jelly. That explains.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a stack from a LinkedList Java Tip java.lang 0 04-16-2008 11:28 PM
Implementing a Stack Using two Queues rhm54 New To Java 2 01-29-2008 04:00 AM
Stack Trace Java Tip Java Tips 0 12-10-2007 06:29 PM
Help with heap and stack coco Advanced Java 1 08-06-2007 03:21 PM
Creating a stack with data ai_2007 Advanced Java 2 07-02-2007 04:28 PM


All times are GMT +3. The time now is 10:40 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org