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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:32:24+00:00 2026-06-06T13:32:24+00:00

I’m trying to make a copy constructor for an object and one of the

  • 0

I’m trying to make a copy constructor for an object and one of the parameters is an ArrayList.

when creating the ArrayList object, I had in mind to use the ArrayList constructor where you can pass a collection as a parameter, but I’m not sure if this will work as a “pointer” to the arraylist or if this will create a whole new arraylist object

This is the code I have

public MyObject(MyObject other)
{
    this.brands= other.brands;
    this.count = other.count;
    this.list = new ArrayList<Integer>(other.list); // will this create a new array list with no pointers to other.list's elements?

}
  • 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-06T13:32:25+00:00Added an answer on June 6, 2026 at 1:32 pm

    I’m not sure if this will work as a “pointer” to the arraylist or if
    this will create a whole new arraylist object

    When you use new, it will create a brand spanking new instance of ArrayList (this is what you have asked). But it will not also automatically create copies of its elements (which I think is what you are looking for). What this means is, if you change a mutable object in the new List, it will also change in the original List, if it is still around. This is because the List only holds references (kinda sorta but not exactly pointers) to the Objects in them, not the actual Objects themselves.

    For example:

    Person person = new Person("Rob"); // create a new Object
    
    List<Person> myList = new ArrayList<Person>();
    myList.add(person);
    
    // Create another list accepting the first one
    List<Person> myList2 = new ArrayList<Person>(myList);
    
    for(Person p : myList2) {
        p.setName("John"); // Update mutable object in myList2
    }
    
    person = new Person("Mary"); // stick another object into myList2
    myList2.add(person);
    
    for(Person p : myList2) {
        System.out.println(p.getName()); // prints John and Mary as expected
    }
    
    for(Person p : myList) {
        System.out.println(p.getName()); // prints only one result, John.
    }
    

    So you can see that the two Lists themselves can be modified independently, but when you use the constructor accepting another List, both will contain references to the same Person instances, and when the state of these objects change in one List, they will also change in the other (kinda sorta just like pointers).

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.