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 8881213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:17:57+00:00 2026-06-14T20:17:57+00:00

I have two classes, say Class Foo , and Class Boo.Foo class contains an

  • 0

I have two classes, say Class Foo , and Class Boo.Foo class contains an arraylist of Boo class. I need to create a deep copy of Foo class, and I’m creating arraylist of Foo itself. How can I accomplish this.

        Class Foo{
        ArrayList<Boo> boovar = new ArrayList<Boo>();
        }

        Class Boo{
        int x;
        void add_x(int y)
        {
            x=y;
        }   
        }

         ArrayList<Foo> foovar = new ArrayList<Foo>();


    ..adding elements to foovar.

now I want to create an ArrayList foovar1 of type Foo, such that it is a deep copy of the objects in foovar. ie, if I modify a boo object in foovar, like deleting x or adding elements, it should not be reflected when i access foovar1.

I know deep copying has been beaten to death, but I really couldn’t find articles telling me how to do this. Could someone show me how with a code?

EDIT:

Im trying to create a copy constructor within Foo, like one of the comments mentioned, but I’m really at a loss on how to copy the Boo arraylist to another through the constructor in Foo.
Not to mention how I’m going to loop the instances of Foo in the arraylist to create copies of that as well. Any help would be really appreciated!

  • 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-06-14T20:17:58+00:00Added an answer on June 14, 2026 at 8:17 pm

    First, you need to make a copy Constructor in your Boo class:

    Class Boo{
      int x;
    
      //copy constructor 
      public Boo (Boo sourceBoo){
        x = sourceBoo.x;
      }
    
    }
    

    with the above, you can make a copy constructor in Foo class that copies the array of Boos contained in it:

    Class Foo{
      ArrayList<Boo> boovar = new ArrayList<Boo>();
    
      //copy constructor
      public Foo(Foo sourceFoo){
        for (Boo aBoo:sourceFoo.boovar){
          boovar.add(new Boo(aBoo)); 
        }
      }
    
    }
    

    now, you can loop over your array of Foo(s) and copy them one by one into a new array. you can define a method to do that:

    public ArrayList<Foo> deepCopyFooList(ArrayList<Foo> sourceArray){
       ArrayList<Foo> targetList = new ArrayList<Foo>();
       for (Foo aFoo:sourceArray){
         targetList.add(new Foo(aFoo));
       }
    
       return targetList;
    }
    

    so to copy your array you would:

    ArrayList<Foo> foovar = new ArrayList<Foo>();
    ArrayList<Foo> foovarCopy = deepCopyFooList(foovar);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two independent classes: class Foo { int bar; } class Baz
Say I have two classes One, the Foo class, interfaces with the user, and
Newbie question. I have two c# classes - a code class (say, CodeClass) and
Say you have two classes, order and customer: public class Customer{ public int CustomerId
Say I have two classes, and neither of them are GUI components. Class A
Let's say we have these two classes: public class Base { public static int
Say I have two separate classes, A and B. I also have Repository class
Let's say I have a base class 'A' and two classes 'B' and 'C',
Say we have a class inheriting from two base classes (multiple inheritance). Base class
Let's say I have two classes like the following: Class A { public: ..

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.