- Home /
Are nested lists bad?
protected List<List<Leaf>> m_Leafs = new List<List<Leaf>>();
I'm working on a procedural house builder. To clarify, the size of the list changes depending on the building it's generating. For example: a bungalow would use a single list, where a semi-detached house will have 2 as each part will be alike a building of it's own.
Should I avoid nested lists, or perhaps are they looked down on?
I'm not sure if they're looked down upon or not. It really depends on the situation, I would say. For this case, you might be better off just using a dictionary, if all the child lists are the same. As you mentioned:
a semi-detached house will have 2 as each part will be alike a building of it's own.
You can use a dictionary, which has much faster lookup speeds, in order to accomplish this, rather than nesting lists. For example:
Dictionary<int, List<Leaf>> m_Leafs = new Dictionary<int, List<Leaf>>();
Then, in the case of a semi-detached building, you just need to add one building, with the key '2', which represents how many copies it has. For example:
m_Leafs.Add(2, building);
You can access it by referring to the key, e.g: List<Leaf> building = m_Leafs[2]
and then assume that you have two copies of the same list. etc.
The way you put it, it would be a waste of memory to duplicate a list and place it in another list. Also, you could use your own custom class to do this instead of a dictionary. Hope that helps. @Chikzter
Answer by Sukender_OB · Apr 18, 2021 at 06:08 AM
Hi @Chikzter,
Every container has its own usage and own drawbacks. Lists of lists are not common, IMHO, but their usage really depend on precisely what you need.
You have to get in mind a few things (complexities and major features) about common containers for this. As an example, unsorted vectors (arrays, lists) are good for sequential reading and random access because data is contiguous in memory. But they aren't for lookup, or frequent insertions & deletions. They also are very bad for insertions / deletions "in the middle" of the vector, since they require moving data.
For reference, you might look for "containers complexities [LANGUAGE]" in your favorite search engine. For instance you may get an eye on:
For C# : https://docs.microsoft.com/en-us/dotnet/standard/collections/
For C++ : https://alyssaq.github.io/stl-complexities/
As a side note, handling building data may need much more than just "leaves". Did you think about using a more complex backend, such as a small database? I personally used SQLite for local projects and found it useful.
Your answer
Follow this Question
Related Questions
Implementation of directional buildings? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to instantiate a list of game objects multiple times? 2 Answers
Serialization Error 1 Answer