Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6744723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:07:04+00:00 2026-05-26T12:07:04+00:00

I have a stack A and I want to create a stack B that

  • 0

I have a stack A and I want to create a stack B that is identical to stack A. I don’t want stack B to simply be a pointer to A — I actually want to create a new stack B that contains the same elements as stack A in the same order as stack A. Stack A is a stack of strings.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:07:05+00:00Added an answer on May 26, 2026 at 12:07 pm

    Just use the clone() -method of the Stack-class (it implements Cloneable).

    Here’s a simple test-case with JUnit:

    @Test   
    public void test()
    {
        Stack<Integer> intStack = new Stack<Integer>();
        for(int i = 0; i < 100; i++)        
        {
            intStack.push(i);
        }
    
        Stack<Integer> copiedStack = (Stack<Integer>)intStack.clone();
    
        for(int i = 0; i < 100; i++)            
        {
            Assert.assertEquals(intStack.pop(), copiedStack.pop());
        }
    }
    

    Edit:

    tmsimont: This creates a “unchecked or unsafe operations” warning for me. Any
    way to do this without generating this problem?

    I at first responded that the warning would be unavoidable, but actually it is avoidable using <?> (wildcard) -typing:

    @Test
    public void test()
    {
        Stack<Integer> intStack = new Stack<Integer>();
        for(int i = 0; i < 100; i++)
        {
            intStack.push(i);
        }
    
        //No warning
        Stack<?> copiedStack = (Stack<?>)intStack.clone();
    
        for(int i = 0; i < 100; i++)
        {
            Integer value = (Integer)copiedStack.pop(); //Won't cause a warning, no matter to which type you cast (String, Float...), but will throw ClassCastException at runtime if the type is wrong
            Assert.assertEquals(intStack.pop(), value);
        }
    }
    

    Basically I’d say you’re still doing an unchecked cast from ? (unknown type) to Integer, but there’s no warning. Personally, I’d still prefer to cast directly into Stack<Integer> and suppress the warning with @SuppressWarnings("unchecked").

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stack which contains some integer data. I want to find out
I'm outside gdb's target executable and I don't even have a stack that corresponds
I have a Stack container class and I want to create instances of various
I actually want to create a new local. I know it sounds dubious, but
i have a stack of messages in database table. i want to send these
We have objects that we want to represent in stacks (think of stacking items
I have a stack panel inside of an expander panel that I programaticaly adds
I have read on Stack Overflow some people that have converting to C#2.0 to
I have a method that tries to create a Uri and then clean it
I stack with this, I'm trying to create application using TabLayout. I have 3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.