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

  • SEARCH
  • Home
  • 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 8842345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:56:03+00:00 2026-06-14T10:56:03+00:00

I have an ArrayList that contains objects. (this objects are clases, every object contains

  • 0

I have an ArrayList that contains objects. (this objects are clases, every object contains an incredibly large amount of information, precisely these object represent large electric generators, so copying the properties one by one is not an option).

example:

ArrayList arr = {ob1, ob2, ob3, ob4, ob5}

So what I try to do is to clone the object (ob1) to the position 5.

arr.set(4, arr.get(0));

But somehow, doing that, ob5 is not a copy ob ob1, it is ob1, so if I change ob5, ob1 changes too.

Is this something inherent to ArrayLists?
Is there any difference if I use Lists instead?

  • 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-14T10:56:04+00:00Added an answer on June 14, 2026 at 10:56 am

    This is because you are doing a shadow copy instead of a deep copy.

    Shallow copy: If a shallow copy is performed on Obj-1 then it is copied but its contained objects are not. The contained objects Obj-1 and Obj-2 are affected by changes to cloned Obj-2. Java supports shallow cloning of objects by default when a class implements the java.lang.Cloneable interface.

    Deep copy: If a deep copy is performed on obj-1 as shown then not only obj-1 has been copied but the objects contained within it have been copied as well. Serialization can be used to achieve deep cloning. Deep cloning through serialization is faster to develop and easier to maintain but carries a performance overhead. source

    An example of Deep Copy:

    class Student implements Cloneable {
      //Contained object
      private Subject subj;
      private String name;
    
      public Subject getSubj() {..}
      public String getName() {..}
    
      public Object clone() {
        //Deep copy
        Student s = new Student(name, subj.getName());
        return s;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an arraylist, that contains model class objects. Here is how I create
I have an object that contains a list of other objects. Say it's as
I have a two-dimension ArrayList that contains double values: ArrayList<ArrayList<Double>> data = new ArrayList<ArrayList<Double>>();
I have an ArrayList that I want to use to hold RaceCar objects that
I have a byte[] that i obtained using Object ArrayList<Obj> Can anyone tell me
I have a class that contains two arraylists which I'm trying to store objects
I have an array list containing objects from the class EventOnMap . Every object
I have a ArrayList with custom objects. I want to search inside this ArrayList
I have an ArrayList containing Car objects and I want to get from that
In the life-cycle of my application, I have to re-create an ArrayList that contains

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.