Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Yaro111222 · Sep 13, 2016 at 10:58 AM · c#arrayslistscopy

Copying a list of lists of int's in c#

Hello, I'm having a bit of trouble with lists, mainly I want to copy contents of one list to another one without leaving any connection between them. But for some reason I'm not able to do that, I've noticed that this happens only with lists of lists. Thanks for any help

  List<int> A = new List<int> ();
     List < List<int>>B= new List < List<int>>();
     List < List<int>>D= new List < List<int>>();
 
     void Start () {
         A.Add (3);
         A.Add (4);
         A.Add (5);
         B.Add (new List<int> (A));
         D = new List<List<int>> (B); //why new doesn't mean new?
         B [0] [1] = 7;
         print (D [0] [1]); //prints 7 and I want 4
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by NerdClown · Sep 13, 2016 at 11:43 AM

//why new doesn't mean new?

I believe the answer to this has to do with references. List B contains REFERENCES to its elements (just one element at this point, namely list A). So when you ask for a new List B, it will make you a brand new list (because you ask for it), but the content of the list will be copied, i.e. the references are copied but still reference the same elements. This is why changing the element in D will also change the element in B.

Anyhow, I guess THIS explains it better!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Sep 13, 2016 at 01:46 PM 2
Share

Yeah

 D = new List<List<int>> (B);

DOES create a new list, but the content of that list is the actual list B you created earlier and feed in as the parameter here.

Change it to

 D = new List<List<int>> (new List<int>(A));

and it should work as you describe. Now your list of lists will contain a new list with the same content as A. Int is a value type so the values in A will not change when you change some int in this new list of ints. The values of ints in list A are copied to the new list, not references to those ints.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is my List adding to same array object again and again in update? 1 Answer

Endless Runner with new Biome each time 0 Answers

Increase List of Spawns Dynamically 1 Answer

All crops in list growing at once when they are supposed to grow individually 0 Answers

Trying to program two buttons to appear when the player in my game dies 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges