2 Replies Latest reply: Feb 23, 2013 12:50 PM by 992891 RSS

    Method Call

    992891
      Hi everyone,

      I'm currently trying to make my own version of the Stack ADT. I have various methods including one called isEmpty(). Maybe this is an obvious question, but I want to use the isEmpty() method that I created in all of my other methods that require it. For example, in the code below, I want to use it in the pop() method in order to test if the stack is empty before I try to pop something from it. The problem is that it's not letting me use the method I created, and instead is telling me to use .length like for a regular array. How do I make it so that it allows me to use the method that I made? Any help appreciated, thanks.

      Sorry for the terrible copy paste btw, but I can't seem to be able to figure out how to paste the code nicely with lines and all.


      public class Stack<T> implements StackADT<T> {

           private T[] array = (T[]) new Object[50]; // type casts object to T[]
           private boolean doubleExpand = true; // initial array expand
           
           public boolean isEmpty(){
                
                for(T elem: array){
                     if(elem != null){
                          return false;
                     }
                }
                return true;
                
           }
           public T pop(){
                
                if (array.isEmpty()){
                //code     
                }     
           }
      }

      Edited by: 989888 on Feb 22, 2013 11:01 PM
        • 1. Re: Method Call
          EJP
          I want to use the isEmpty() method that I created in all of my other methods that require it.
          So call it.
          For example, in the code below, I want to use it in the pop() method in order to test if the stack is empty before I try to pop something from it. The problem is that it's not letting me use the method I created
          You didn't create it. You can't create methods in the array classes.
          instead is telling me to use .length like for a regular array.
          QED. It is a regular array. You are calling methods on an array, not one of your own classes.
          • 2. Re: Method Call
            992891
            I feel somewhat retarded right now.

            Thanks for your help