Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree2Likes

Thread: Reversing a Stack

  1. #1
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default Reversing a Stack

    Hello Friends

    I am trying to Reverse a Java stack, I realise that there isn't a .reverse() method for that class. And as far as I now, the only way to do it would be to go through each element one by one, and place it into another one.

    The problem with that is, the .pop() method keeps removing Objects from the original stack, and I want to keep them there ! I'm at a loss.

    I would greatly appreciate any help. Here's what I had before, but like I said it destroys the original stack.

    Java Code:
    //Reverse Stack
    Stack<Node<Adjustment>> op = new Stack<Node<Adjustment>>();
    int stackSize = opm.getOptimumPath().size();
    for(int i = 0; i < stackSize; i++)
    {
        op.push(opm.getOptimumPath().pop());
    }
    Note: getOptimumPath() returns as Stack.

    Thanks

  2. #2
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Can you use any of the Vector class methods? Stack extends Vector

  4. #4
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    Also have a look at this class: Deque (Java Platform SE 6)

    Stack is very old while Dequeu is newer and better.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default

    Copy your Stack (hint: use the addAll( ... ) method on a new, empty Stack) and use that copy to create your reversed stack.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Hi guys, thanks for the replies so far.

    @OutputStream: I've just taken a quick look at the Java Deque class, and you're right ! it appears to make more sense. So I went into one of my classes and replaced everything that was Stack, with Deque and now I'm getting an error, that a Stack did not give me !!

    Java Code:
    /**
    * Clone this message
    */
    public Object clone() throws CloneNotSupportedException {
        return new OptimumPathMessage((Deque<Node<Adjustment>>) godPath.clone());
    }
    The above code is giving me an error saying:

    The method clone() is undefined for the type Deque<Node<Adjustment>>
    Replace Deque with Stack and I don't have this problem !! The cloning is important in my code Plus for the Stack, I hadn't defined any clone() method !! it just worked !

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Deque is an interface, not a class.

  8. #8
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Sorry Norm. You're right it is an Interface. But does that mean I am using it in the wrong manner? I typed "Java Deque Examples" into google, and got some code, that was using it just like any other class !!

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Sorry, I can't see your code from here. Look at the API doc for Deque. It lists several classes that implement it.

  10. #10
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Sorry, I can't see your code from here. Look at the API doc for Deque. It lists several classes that implement it.
    aha ArrayDeque then !! that seems to work ! Thanks Norm !

    One things remains, how do I create a different ArrayDeque object that has the same element(s) as another one ? Our good friend Josh mentioned the addAll() method. Here's what the API says about the addAll() method:

    public boolean addAll(Collection<? extends E> c)

    Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)

    This implementation iterates over the specified collection, and adds each object returned by the iterator to this collection, in turn.
    I'm sometimes terrible at fully grasping what the API is exactly saying. My understanding of the above would be something like:

    Java Code:
    ArrayDeque<Node<Adjustment>> AD01 = new ArrayDeque<Node<Adjustment>>(); // has elements in it
    
    ArrayDeque<Node<Adjustment>> AD02 = new ArrayDeque<Node<Adjustment>>(); // has no elements in it
    
    //Moving the elements from AD01 to AD02
    AD01.addAll(AD02)
    Is the above right ?

    Thanks, you guys are so helpful.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    When in doubt after reading doc, write a small test program and try it to see what happens and how to use it.
    Dark and Ciwan like this.

  12. #12
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Hi Norm.

    I just did try it in my program. Although Eclipse isn't giving any errors, the content (elements) of ArrayDeque one, aren't being copied to ArrayDeque two. Here's the code:

    Java Code:
    OptimumPathMessage opm = (OptimumPathMessage)getMessage();
        	
        	ArrayDeque<Node<Adjustment>> ArrayD = new ArrayDeque<Node<Adjustment>>();
        	opm.getOptimumPath().addAll(ArrayD);
    What am I missing ? I've gone over the API again for ArrayDeque and that addAll() method, but I can't see what I'm doing wrong

    Thanks

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Can you make a small test program that compiles and executes to demonstrate the problem. Your code snippet doesn't show what happens when it is executed.

  14. #14
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Do I have to ? In the dubgger I'm adding a stop point right after that code, this way I give myself the chance to check ArrayD before going any further. And ArrayD is empty.

    If you insist, I'll go create a little program that does the above, but with some println statement.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Its hard to test code by looking at it. Especially when there is a lot more than what is posted.
    An executable program means that we're talking about the same thing. The same input etc.
    When I execute what you post I should get the same results as you do. From there I can debug the code.

  16. #16
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Ahh now I got you, it makes sense actually !!

    Right here's the content of my class JavaPlay:

    Java Code:
    import java.util.ArrayDeque;
    
    public class JavaPlay {
    
    	private ArrayDeque<String> x;
    	private ArrayDeque<String> y;
    	
    	public JavaPlay()
    	{
    		x = new ArrayDeque<String>();
    		y = new ArrayDeque<String>();
    	}
    	
    	public void fillArrayX()
    	{
    		x.addLast("Hi");
    		x.addLast("My");
    		x.addLast("name");
    		x.addLast("is");
    		x.addLast("Ciwan");
    	}
    	
    	public void copyStuff()
    	{
    		x.addAll(y);
    	}
    	
    	public void printArrayX()
    	{
    		System.out.println("Array X:" + x);
    	}
    	
    	public void printArrayY()
    	{
    		System.out.println("Array Y:" + y);
    	}
    }
    And here's the Main class:

    Java Code:
    public class Main {
    
    	public static void main(String[] args) {
    		
    		JavaPlay x = new JavaPlay();
    		x.fillArrayX();
    		x.printArrayX();
    		x.copyStuff();
    		x.printArrayY();
    	}
    
    }
    And here's the console output:

    Array X:[Hi, My, name, is, Ciwan]
    Array Y:[]

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    You didn't say whether the output from the test program was what you wanted or if there was a problem with it.
    Does your test program do what you want it do?
    If not please explain what you want it to do.

  18. #18
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    The output I got was this:

    Array X:[Hi, My, name, is, Ciwan]
    Array Y:[]
    What I was expecting, was this:

    Array X:[Hi, My, name, is, Ciwan]
    Array Y:[Hi, My, name, is, Ciwan]
    I thought that was clear from my earlier posts sorry I guess I should have made it clearer.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Try this: y.addAll(x);

    y is empty. x has the data you want to put in y.

  20. #20
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    LinkedList implements Deque, and it has a copy constructor.
    Get in the habit of using standard Java naming conventions!

Page 1 of 2 12 LastLast

Similar Threads

  1. Reversing the String
    By Inaam in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 08:35 PM
  2. Reversing
    By whosadork in forum New To Java
    Replies: 14
    Last Post: 11-06-2008, 04:29 AM
  3. Reversing String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-02-2007, 09:42 PM
  4. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:24 PM
  5. reversing a string
    By toad in forum New To Java
    Replies: 5
    Last Post: 11-07-2007, 09:13 AM

Posting Permissions

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