Closed Thread
Results 1 to 3 of 3

Thread: Help with Stacks

  1. #1
    gammaman is offline Learning Programmer
    Join Date
    Oct 2008
    Posts
    70
    Rep Power
    0

    Help with Stacks

    This is my first time working with stacks. I am trying to get familiar with using some of the methods from java.util.Stack. Specifically i am trying to set the size of the stack and then, using a for loop, push items onto the stack until the stack has reached its capacity. However I seem to be having some trouble,
    sometimes my output looks like [null,null,null... 0,2,4...] depending on the size, and sometimes (when I change the code to try something else) I just get
    [null,null,null,......]. What I would like to do is populate the stack with numbers [1- whatever the size of the stack is], then on the next iteration of the loop I want it to reach and print that the stack is full.

    Code:
     import java.util.Stack;
     
     
    public class myStack{
        
     private static Stack stack = new Stack();
     
     public static void main(String[] args) {
         stack.setSize(5);     
          
          //System.out.println(stack.size());
          //System.out.println(stack.empty());
         
      
          for(int i= 0; i<stack.size(); i++){
              if(!stack.isEmpty()){
                       stack.push(i);
                             }
              else{
                   System.out.println("stack is full");
     
                 
              }
              i+=1;
          }
          System.out.println(stack);
          
        
      }
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jan 2008
    Posts
    1,725
    Blog Entries
    4
    Rep Power
    29

    Re: Help with Stacks

    Just a tip, inside your for loop, you call stack.size() each time you iterate through that loop which makes your program slightly less efficient.

    Now onto programming theory. Stacks are implemented where you can either pop or push an element to/from the stack. By setting the size, you are making 5 null pointers in the stack because it pushes 5 nulls down on. Here is what you really need:

    Code:
     import java.util.Stack;
     
     
    public class main{
        
     private static Stack stack = new Stack();
     
     public static void main(String[] args) {    
         
    	 int size = 5;
    	 
          //System.out.println(stack.size());
          //System.out.println(stack.empty());
         
          for(int i= 0; i<size; i++){
        	  stack.push(i);
          }
          System.out.println(stack);
          
        
      }
    }

  4. #3
    gammaman is offline Learning Programmer
    Join Date
    Oct 2008
    Posts
    70
    Rep Power
    0

    Re: Help with Stacks

    Thank you, that was so simple. Sometimes I tend to overlook the obvious.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help with assignment - stacks
    By moey187 in forum C and C++
    Replies: 1
    Last Post: 09-13-2011, 05:43 AM
  2. Intermediate C# Stacks
    By chili5 in forum CSharp Tutorials
    Replies: 2
    Last Post: 08-22-2011, 02:35 PM
  3. Problem with Stacks in C
    By TheUmer in forum C and C++
    Replies: 22
    Last Post: 09-06-2009, 03:53 PM
  4. Stacks
    By chili5 in forum Java Tutorials
    Replies: 5
    Last Post: 03-07-2009, 06:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts