Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Method returns empty stack when called

  1. #1
    Junior Member pyler's Avatar
    Join Date
    Sep 2012
    Posts
    23
    My Mood
    Busy
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Method returns empty stack when called

    I have a stack method that's supposed to return a reversed *copy* of the this object. I need this object to link to that object. Thanks.

    To clarify, the that stack object that's created pushes items that are popped from the this object. I want the this object to reference the that object after the this object gets empty. What I really want is to return a reversed copy of the this object. Clear?

    public LinkedStack<E> reversed()
    {
        LinkedStack<E> that= new LinkedStack<E>();
        if(this.isEmpty()){
            return this;
        }
        else{
            while(top!=null)
                {
                    that.push(pop());
                }
            }
        return this;
        }

    Currently when I create a LinkedStack object, say stack1 and push items onto it, stack1.reverse() doesn't return a reversed copy of stack1. It returns an empty stack instead.
    What I'm looking for is for a way to reference that in the reverse method.
    Last edited by pyler; October 7th, 2013 at 12:23 AM. Reason: to prettify code


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Method returns empty stack when called

    Hello.
    Obviously it will return empty stack as per your logic.
    Think if you need to return this or that.

    Syed.

  3. #3
    Junior Member pyler's Avatar
    Join Date
    Sep 2012
    Posts
    23
    My Mood
    Busy
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Method returns empty stack when called

    I want it to return a copy of the this object but with the contents in reverse order. I want to link this to that after all the items from this have been popped, but I can't find the right keyword.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Method returns empty stack when called

    Do you understand why your implementation empties the object? If you do not, then my advice would be to stop and spend some time understanding why. Do you know how to add them back? Have you tried reversing the underlying data structure which contains the E elements (which you haven't told us about)?

  5. #5
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Method returns empty stack when called

    that.push(pop());

    Doesn't that empty your original stack in the process?

    I cannot think of any way using only Stack methods to create a reverse Stack that doesn't destroy the original one. (There might be a way, but I just can't think of one.)

    Assuming, per your title of the class that you're using a LinkedList, not sure if it's a doubly linked list or a singly linked list or if you're using java's LinkedList class or one of your own, there is a way to get the end of the list, or the bottom.

    You'd have to clone your first Stack. Then, using its LinkedList setup, you'd have to use getFirst() and getLast() and removeFirst() and removeLast() and addFirst() and addLast() through the list till you reach the middle, at which point it will have been flipped around.

  6. #6
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Method returns empty stack when called

    Double posted here thread locked due to it being in a different post as well

    As I had asked in that post how are you calling your method? Not only that you are now returning an empty stack and not a newly created stack which has reversed elements in it.

    Think of calling this method as baseball.
    1. You pitch the ball (Call the method)
    2. The batter hits the ball (Method returns something)
    3. Someone catches the ball (Value is assigned to something)

    If you are missing one then you won't have your desired results. The reason I am guessing your other version wasn't working was because you were missing step number 3. You were probably just calling Object.reverse() which it was returning the ball but there was no catcher so the ball got lost and you were left with the original stack you called .pop() on.

Similar Threads

  1. equals method is never called
    By justme1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2013, 11:39 PM
  2. method always returns 0
    By bdennin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 26th, 2013, 05:31 AM
  3. toggle colors from an array when the method is called.
    By samjoyboy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 06:00 PM
  4. Creating Method that returns a casted object type
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 31st, 2012, 01:23 PM
  5. Replies: 1
    Last Post: February 12th, 2012, 01:01 AM

Tags for this Thread