how to copy a stack to a other stack without destorying....


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: how to copy a stack to a other stack without destorying....

  1. #1
    Jay008 Guest

    how to copy a stack to a other stack without destorying....


    how to copy a stack to a new stack without destorying the original one?
    the stack is an array. simple code please.

    I have tried something like this, but the old one is gone.
    /*
    stack temp=new stack(a.count);
    while(!a.isEmpty()
    {
    temp.push(a.pop())
    }

    return temp;
    */


  2. #2
    Ruchi Dhar Guest

    Re: how to copy a stack to a other stack without destorying....


    Stack extends from Vector so you can use all the methods available in Vector.
    The simplest way of copying stack is to clone it

    Stack temp = a.clone();

    Other wise get all elements in the stack by calling method toArray() and
    copy each of them into new stack;

    Stack temp = new Stack(a.size());
    Object[] obj = a.toArray();
    for(int i=0; i<obj.length; i++)
    temp.push(obj[i]);

    "Jay008" <mph100@hotmail.com> wrote:
    >
    >how to copy a stack to a new stack without destorying the original one?
    >the stack is an array. simple code please.
    >
    >I have tried something like this, but the old one is gone.
    >/*
    >stack temp=new stack(a.count);
    >while(!a.isEmpty()
    >{
    > temp.push(a.pop())
    >}
    >
    >return temp;
    >*/
    >



  3. #3
    Jay008 Guest

    Re: how to copy a stack to a other stack without destorying....


    If the original stack "a" is also an array. will your 2nd method still work?
    by the way, thanks alot!

    "Ruchi Dhar" <rdhar@verticalnet.com> wrote:
    >
    >Stack extends from Vector so you can use all the methods available in Vector.
    > The simplest way of copying stack is to clone it
    >
    >Stack temp = a.clone();
    >
    >Other wise get all elements in the stack by calling method toArray() and
    >copy each of them into new stack;
    >
    >Stack temp = new Stack(a.size());
    >Object[] obj = a.toArray();
    >for(int i=0; i<obj.length; i++)
    > temp.push(obj[i]);
    >
    >"Jay008" <mph100@hotmail.com> wrote:
    >>
    >>how to copy a stack to a new stack without destorying the original one?
    >>the stack is an array. simple code please.
    >>
    >>I have tried something like this, but the old one is gone.
    >>/*
    >>stack temp=new stack(a.count);
    >>while(!a.isEmpty()
    >>{
    >> temp.push(a.pop())
    >>}
    >>
    >>return temp;
    >>*/
    >>

    >



  4. #4
    Ruchi Dhar Guest

    Re: how to copy a stack to a other stack without destorying....


    If "a" is an array best thing is to use System.arrayCopy() it is optimized
    for copying arrays. I wrote the below one thinking that there is a stack
    called "a".

    "Jay008" <mph100@hotmail.com> wrote:
    >
    >If the original stack "a" is also an array. will your 2nd method still work?
    >by the way, thanks alot!
    >
    >"Ruchi Dhar" <rdhar@verticalnet.com> wrote:
    >>
    >>Stack extends from Vector so you can use all the methods available in Vector.
    >> The simplest way of copying stack is to clone it
    >>
    >>Stack temp = a.clone();
    >>
    >>Other wise get all elements in the stack by calling method toArray() and
    >>copy each of them into new stack;
    >>
    >>Stack temp = new Stack(a.size());
    >>Object[] obj = a.toArray();
    >>for(int i=0; i<obj.length; i++)
    >> temp.push(obj[i]);
    >>
    >>"Jay008" <mph100@hotmail.com> wrote:
    >>>
    >>>how to copy a stack to a new stack without destorying the original one?
    >>>the stack is an array. simple code please.
    >>>
    >>>I have tried something like this, but the old one is gone.
    >>>/*
    >>>stack temp=new stack(a.count);
    >>>while(!a.isEmpty()
    >>>{
    >>> temp.push(a.pop())
    >>>}
    >>>
    >>>return temp;
    >>>*/
    >>>

    >>

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center