Jump to content

Recommended Posts

ok this is what i have so far. 

 

  import java.util.ArrayList;
  import java.util.EmptyStackException;

          public class Stacked
          {
             protected int top = -1;
             ArrayList stacked = new ArrayList();
             
             
             
             public int size()
             {
                 return (top + 1);
             }  
                         
             public ArrayList top() throws EmptyStackException
             {
                 if (isEmpty())
                   throw new EmptyStackException();
                 return stacked[top];
             }
                
              public void push(Object obj) 
             {  
                stacked.add(obj);
             }

             public Object pop() 
             {
                 
                if (stacked.isEmpty())
                   throw new EmptyStackException();
                return stacked.remove(stacked.size()-1);
             }
             
             public boolean isEmpty() 
             {
                  
                return stacked.isEmpty();
             }
             
          } 

 

i get this error " array required, but java.stack.util.ArrayList found"  what is going on problem with this "return stacked[top];".

 

what iam trying to do is make a stack using an arraylist. anyhelp or pointers on the overall code so far will be much apreciated iam new at java.

Link to comment
https://forums.phpfreaks.com/topic/73422-simple-java-programm/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.