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 /
avatar image
0
Question by unity_c3Z8hg6NHcx8OA · Dec 05, 2018 at 05:15 PM · scripting problemlist of lists

list.Add is replacing

Im trying to generate a list of lists of lists of ints List<List<List<int>>>();. But every time it is suppose to add, it is replacing all items before.


 int numeroDeInputs = 2;
     int numeroDeOutputs = 2;
     int numeroDeCamadas = 6; //layers
 
     List<int> genes;
     List<List<int>> GeneList;
     List<List<List<int>>> genotipo;
 
 
 
     void Start()
     {
         genotipo = new List<List<List<int>>>();
         GeneList = new List<List<int>>();
         genes = new List<int>();
 
         StartGenotipo();
         PrintGenotipo();
     }
 
     void StartGenotipo () {
         genotipo.Clear();
         for (int i = 0; i < numeroDeCamadas-1; i++)
         {
             GeneList.Clear();
             int tamanhoDoProx = numeroDeCamadas - i - 2 + numeroDeOutputs; //size of next layer
             for (int j = 0; j < tamanhoDoProx+1; j++)
             {
                 genes.Clear();
                 for (int k = 0; k < tamanhoDoProx; k++)
                 {
                     genes.Add(Random.Range(0, tamanhoDoProx - 1));
                 }
                 GeneList.Add(genes);
             }
             genotipo.Add(GeneList);
         }
     }
     void PrintGenotipo()
     {
         string retorno = "";
         foreach (List<List<int>> lsLsI in genotipo)
         {
             retorno += "\n(";
             foreach (List<int> lsI in lsLsI)
             {
                 retorno += "\n[";
                 foreach(int num in lsI)
                 {
                     retorno += num;
                 }
                 retorno += "]";
             }
                 retorno += ")";
         }
         Debug.Log(retorno);
     }


it should output in the console something like:

 ([015031][012350][130350])
 ([01234][42023][01210])
 ([0033][1020][1230])
 ([002][012][110])
 ([01][10][11])

instead of

 ([00][00][00])
 ([00][00][00])
 ([00][00][00])
 ([00][00][00])
 ([00][00][00])
 
 




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
2

Answer by Bonfire-Boy · Dec 05, 2018 at 06:08 PM

When you add a list to a list, it adds a reference, not a copy. You're not making a List of lists of lists at all, you're making a list of multiple references to the same list (...of multiple references to the same list.. etc).

So there are 2 fixes that spring to mind. You could try replacing

genes.Clear();

with

genes = new List<int>();

(and so on)

Alternatively, you could change your Add(list) calls so that they're adding a copy.

Comment
Add comment · Show 2 · 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 unity_c3Z8hg6NHcx8OA · Dec 05, 2018 at 08:11 PM 0
Share

I did't quite get it, but it works! thanks!

avatar image Bunny83 unity_c3Z8hg6NHcx8OA · Dec 06, 2018 at 04:24 AM 0
Share

A List is an object. How many List objects do you create? You create exactly 3:

 genotipo = new List<List<List<int>>>();
 GeneList = new List<List<int>>();
 genes = new List<int>();

All those 3 lists are empty in the beginning. When you do

 GeneList.Add(genes);

The GeneList will contain one element, that is a reference to the genes list. There are still only those 3 lists you have created so far. Objects don't magically multiply itself. Objects need to be created with "new".


Bonfire has explained it already quite well. If you have still trouble understanding what's happening i strongly recommend to look up some basic C# / program$$anonymous$$g resources. Understanding what a reference type is is an absolutely crucial detail.

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

170 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 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

Checking a list containing another list, and checking if the elements in the list are equal problem 1 Answer

How can the first C# script get the second C# script data? 1 Answer

iterate through two lists element by element 1 Answer

Networking Buttons and Syncvar 1 Answer

How to make a gun autoaim on targets under the reticule? 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