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
0
Question by bdalziel · May 22, 2018 at 10:46 PM · dictionarygeneric list

For a dictionary with lists as values, changing value associated with one key changes value for all other keys?

The code below is intended to update the values in a dictionary called BucketDict, that has integer keys and list of integers as values. But it appears as if editing the value for one key edits the rest as well?

Resetting the dictionary:

 BucketDict.Clear();
 
 List<int> val = new List<int>();
 for (int i = 0; i < nBucket; i++)
 {
     BucketDict.Add(i, val);
 }


Updating values:

 int key;
 int x;
 int y;
 
  List<int> val = new List<int>();     // intended to hold some value from the dictionary
 
  for (int i = 0; i < MaxCommunitySize; i++) {
   
          x = (int)(LocationX[i] * BucketConversionFactor);
          y = (int)(LocationY[i] * BucketConversionFactor);
 
          key = x + y * ArenaLengthInBuckets;
 
          val = BucketDict[key];     //intended to 'write over' previous value 
          val.Add(i);
 
          Bucket[i] = key;
          BucketDict[key] = val;
 

}

Comment
Add comment · Show 2
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 Ahndrakhul · May 23, 2018 at 12:59 AM 0
Share

I don't understand exactly what you're trying to do here. What are you expecting to see? Lists are reference types, so every time you iterate through the for loop, you are making 'val' refer to the entire list referenced by 'key' in the 'BucketDict' dictionary and then you are adding another int to the list with the value of i.

BucketDict[key] - is a list of ints. Let's say that it equals 1,2,3,4,5 val = BucketDict[key] - makes val refer to the list above val.add(i) - adds the value of i to the end of the above list. If i is 2 then the list is now 1,2,3,4,5,2 BucketDict[key] = val - doesn't really do anything because val is already a reference to BucketDict[key]

In your example here, you would get the same result if you got rid of the val list and, replaced line 16 with BucketDict[key].add(i);

avatar image bdalziel · May 25, 2018 at 06:52 PM 0
Share

Thanks @Ahndrakhul and see answer below. Thanks regarding the efficient way to write line 16 as well.

1 Reply

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

Answer by bdalziel · May 25, 2018 at 06:47 PM

@Ahndrakhul thanks for responding to a confused question. What you said did lead me to the answer (I have also edited the question to make it clearer what my problem was):

The overall problem was that I needed to read about reference types versus value types in C#. Now that the problem is fixed I can see that the problem was where the dictionary was 'reset', and that code was not included in the original question - it is now.

I was trying to initially set each value in the dictionary to an empty list, but because list is a reference type, I had set each value to the same list, so subsequently changing one entry in the dictionary changed the others as well.

When resetting the dictionary, declaring a new list in each iteration of the for loop fixes the problem

  BucketDict.Clear();
   
  for (int i = 0; i < nBucket; i++)
   {
       List<int> val = new List<int>();
       BucketDict.Add(i, val);
   }







Comment
Add comment · 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

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

139 People are following this question.

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

Related Questions

Add listener on Objects that are part of dictionary 1 Answer

I've been trying to have a dictionary look up for a word entered by the player. The words.ContainKey(word) is always returning false, even when it should return true. 1 Answer

Issue with printing lines from a dictionary to Unity console. 1 Answer

Need help to generate a random assortment of 4 buttons - C# 0 Answers

[SOLVED] Converting a Generic List to JSON in Unity 3 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