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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:22:59+00:00 2026-06-03T08:22:59+00:00

So i mostly program in C++, and java is very similar to c++. I

  • 0

So i mostly program in C++, and java is very similar to c++.
I have created a list command as such

 List<Item> stuff =new ArrayList<Item>();

where Item is a custom class that basically stores data.
I get information from a text file and store it to Item.
Then i use stuff.add to get the item class to stuff.
Afterwards i use a command to erase all data from the item class and see that it has also deleted all the data from the list.
Basically I want to know if there is a way to add a copy of the class and not the address of the class itself.

Edit:
so i found that reinitializing the item class also solved my problem thanks though.

  • 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-03T08:23:01+00:00Added an answer on June 3, 2026 at 8:23 am

    You want to clone the item before adding it to the list. The list just has a reference to the original item that you created. As such, when you mutate your item, the item in the list is also affected (it’s the same object).

    If you want to “disconnect” the item that you put into your list, then you can clone it first. Your item class will need to implement Cloneable and override the clone() method. If you have only primitives in Item, then you can simply do:

    public Object clone()
    {
      return super.clone();
    }
    

    If you have other objects in Item, then the default clone operation will only make a shallow copy, and you will need to make your clone method more extensive so that it makes deep copies. From the Javadoc for Object.clone():

    By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal “deep structure” of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

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

Sidebar

Related Questions

I have program in Java that I want to rewrite (GUI and some new
I have a Java program that is mostly GUI and it shows data that
This is the situation : I mostly program C# and have written types in
I program mostly in scala and java, using scalatest in scala and junit for
I have a list of strings (company names, in this case), and a Java
I have been programming in Java since 2004, mostly enterprise and web applications. But
I am currently writing a non-web program using mostly python and have gotten to
I have a Java program which has a number options it allows the user
When writing a Java program, do I have influence on how the CPU will
I have a speed question for Java. I am making a chess program, and

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.