- Home /
Add a list within a list.
How would you go about adding a container to a list? Each container will have a list inside. For example I have a star system #1 with list of all the planets, then star sytem #2 with a list of all the planets etc, then planets with a list of all their satellites.
List StarSystem = new List(StarSystem1, StarSystem2, StarSystem3); // Star Systems List
StarSystem1 would have a planet list connected to it, so will the other systems
List StarSystem1 = new List(Planet1, Planet2, Planet3, Planet4); // planets list
Planet1 will have a list of all the satellites and so will the others.
List Planet1 = new List(Sat1, sat2, sat3); // Satellite list
First i need to know what this is called in the area where it says myownclass, its not a string, not an integer. Who knows, maybe the coding is correct. I also need an example what the best way is to retrieve and insert the data.
What do you mean here?
First i need to know what this is called in the area where it says myownclass, its not a string, not an integer.
forgot to add something
List<What do i put here?> StarSystem = new List<What do i put here? >{StarSystem1, StarSystem2, StarSystem3};
That kind of list makes use of generics. This is a way of applying the same concepts and data structures to many different types, while still retaining standard typing regulations. The syntax for generic classes and methods is like this:
Generic<Type>
Here, Generic
is the type of the generic class or method, in this case, List
, while Type
is the type of the object associated with that generic object. In this case, this is the type of the objects that are stored in the list, so if you wanted a list of integers, you would write:
List<int>
(Note that this is allowed in C#, but some languages, like Java (I'm not talking about Javascript here, but actual Java), don't support primitive types like int with generics).
In C#, this can be pretty much any type, so let's say you wanted a list of lists of integers, you would write:
List<List<int>>
What if you wanted a list of lists of booleans? No problem:
List<List<bool>>
What about classes? What if you want a list of lists of Transforms, for example? Well:
List<List<Transform>>
It's as simple as that! You just need to fill in the type of the objects you want to store in the list!
Answer by d112570 · Nov 05, 2013 at 03:35 PM
Finally found a way. Since I had to find a way around activation objects without finding them, I made this code, it will work for anything else I need. 1) Find all my 810 planets at start, which is very fast. I use the same for my 90 stars. 2) Now your able to turn them off and on when ever you like. The code is easy and short Here we initialize is:
public GameObject [,,] mainPlanetList = new GameObject [10,10,10];
void Start () {
findMainPlanetFolder();
}
void findMainPlanetFolder() {
for (int g = 0; g <=9; g++){ // Galaxy
for (int s = 0; s <=9; s++){ // Star
for (int p = 0; p <=9; p++){ // Planet
mainPlanetList[g,s,p] = GameObject.Find("Galaxies/" + g + "/Stars/" + s + "/Planets/" + p + "/Main");
}}}}
void updatePlanetFolder() { // Here I disable and enable the Planet.
for (int p = 1; p <=9; p++){
if (mainPlanetList[activeSystemM[0],activeSystemM[1],p]) { //
if (activeSystemM[2] == p) mainPlanetList[activeSystemM[0],activeSystemM[1],p].SetActive(true); // On
else mainPlanetList[activeSystemM[0],activeSystemM[1],p].SetActive(false); // Off
}}}
activeSystemM is the location I am, 0 for galaxy, 1 for star, 2 for planet, 3 for satellite, and tells which galaxy/Star/Planet I am currently on.
The only thing I see here is that you, my friend, are not from planet Earth.
You really should take the comments that have already been stated in counsel.
Answer by robhuhn · Oct 15, 2013 at 03:10 PM
A generic list contains items of a specific type e.g. Planet, StarSystem, Sat in your case. The type declaration for the list would be then:
List<StarSystem> starSystems = new List<StarSystem>();
List<Planet> planets = new List<Planet>();
List<Sat> satellites = new List<Sat>();
If you want to place one list in another it would look like:
List<List<List<Sat>>> starSystems = new List<List<List<Sat>>>();
starSystems[0] //is a list containing a list with satellites (a starSystem)
starSystems[0][0] //contains a list of satellites (a planet)
starSystems[0][0][0] //contains a satellite
This is really ugly so I would recommend to create some Objects and encapsulate them e.g.
StarSystems[0].Planets[0].Satellites[0]...
I copied the first 3 codes, but I get errors.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
List<StarSystem> starSystems = new List<StarSystem>();
List<Planet> planets = new List<Planet>();
List<Sat> satellites = new List<Sat>();
Error says starsystem could not found, is this not the area where you initialize it?
...
Right, in that case, you shouldn't be program$$anonymous$$g yet - have a look at some beginner C# tutorials.
I feel I should add that just because you've been playing with C# for a year doesn't mean you're not a beginner. In fact, even if you find beginner tutorials boring, it doesn't mean you're not a beginner. The fact that you don't seem to understand some really fundamental concepts means that you are a beginner. You may be able to work with if statements and simple loops okay, but if you got confused by using the phrase "creating a class", then you are a beginner, no matter how long you've been studying. Simple if statements, etc. alone are not enough to make your proficient at the language.
I recommend you stop, take a step back, and do what I've told you and study up on some of the basic concepts, maybe look at some of the C# documentation (most of which is excellent). This is really trivial stuff.
I'm not asking you to give up, I'm asking you to actually learn a little about the language you're using. It's one thing to be able to use ifs, while loops, for loops, etc., but quite another to actually understand the program flow, and what everything actually does. It's that last bit that you're lacking, and you really do need to understand that, or you will not learn. Yes, I could tell you how to fix this problem, and yes, it would work, but if I do, you will not understand why it fixes the problem, or how it works, meaning that you'll just be back here soon enough asking a similar question. The best programmers don't just know how to do something, they know why they do things as well, and with that knowledge, they can work out what to do in situations they aren't familiar with, simply by reasoning about how the language and the program works.
You don't need to go forward, because in this direction, you'll end up repeating the same mistakes due to a lack of fundamental understanding. This isn't just things like "what an if statement does", it's things like: "What is a class?" "How does inheritance work?" "What trade-offs are involved with different methods of achieving certain results?" (for example, in a given situation, is an abstract class or interface better? How about List or LinkedList?) "What does this error mean, exactly?" etc. These are fundamental questions that you really need some idea about. I'm not asking you to go backwards, what I'm telling you do is deepen your understanding before you rush blindly forwards - you will get better results, in a shorter time, this way.
Answer by mattssonon · Oct 15, 2013 at 02:58 PM
To put a list inside i list you do this:
List<List<Class>> StarSystem = new List<List<Class>>(StarSystem1, StarSystem2, StarSystem3);
what do i put in the class? I got this error that class could not be found.
The data type of the list's contents. What kind of data type is Satellite? Is it its own class?
I tried this
List<string> Planet3 = new List<string>{"Phobos","Deimos","","","","","","","",""};
List<List<string>> StarSystem = new List<List<string>>(Planet1, Planet2,Planet3);
got error, planets are a list of names. Should it not be curly brackets? the error reports " does not contain a constructor takes 3 arguments."
Answer by apocaliptika · Jan 07, 2018 at 12:01 AM
This is an old post but it's a common issue. You can have a List>
I had a dictionary originally which worked for a while but wasn't efficient for dynamically populating drop down menus among other things.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
protected Scene thisScene;
protected List<GameObject> rootObjects;
public List<GameObject> planetList;
public List<GameObject> orbitList;
public List<GameObject> levelsList = new List<GameObject>();
public List<List<GameObject>> planetsListWithLevels = new List<List<GameObject>>();
//public Dictionary<string, GameObject> levelsDictionary;
public void GetSolarSystem() {
thisScene = SceneManager.GetActiveScene(); // get the scene
rootObjects = new List<GameObject>(); // list or root objects
thisScene.GetRootGameObjects(rootObjects); // get the root objects
orbitList = new List<GameObject>(); // list of my orbits
planetList = new List<GameObject>(); // list of my planets
levelsList = new List<GameObject>(); // list of my levels
planetsListWithLevels = new List<List<GameObject>>(); // list of level lists
//levelsDictionary = new Dictionary<string, GameObject>(); //alternate way of storing via dictionary
foreach (GameObject obj in rootObjects) { // for every root object
if (obj.name.ToLower().Contains("orbitplanet")) { // if it has orbitplanet inname
levelsList = new List<GameObject>(); // list of my levels
orbitList.Add(obj); // add it to my orbits
planetList.Add(obj.transform.GetChild(1).gameObject); // add the the second child to my planets
foreach (Transform level in obj.transform.GetChild(1)) { // go through planet's children
levelsList.Add(level.gameObject); // add the levels to levels list
//levelsDictionary.Add(obj.transform.GetChild(1).name + level.gameObject.name, level.gameObject); // alternately add to <string,GameObject> dictionary
}
planetsListWithLevels.Add(levelsList); // add my list to the the list of lists
}
// this is to verify the list of lists
for (int i=0;i<planetsListWithLevels.Count;i++){ // for each list
for (int j=0;j<planetsListWithLevels[i].Count;j++) { // for each item in list
print("list : "+i +" level name : "+ planetsListWithLevels[i][j].name); // prints "list : 0 level name : Level1" ... etc
}
}
}
}
Answer by killerwol · Dec 27, 2013 at 08:38 AM
Or you can do something like this, sorry did not use correct programming naming conventions but you get the point. hope that helps out
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Test : MonoBehaviour {
[System.Serializable]
public class MySweetData {
public List<int> IntTest;
}
public List<MySweetData> tested;
// just a little data test trial
void Start ()
{
if (tested.Count == 1)
{
if(tested[0].IntTest.Count ==1)
int d = tested[0].IntTest[0];
else
Debug.Log("IntTest is empty");
}
else
{
Debug.Log("test is empty");
}
}
This is not a good solution. It provides unnecessary bloat and the only gain it gives is an additional name, which in itself is questionable.
Your answer
Follow this Question
Related Questions
Boo Arrays & Lists 1 Answer
Create game objects with unique IDs and detect them? 1 Answer
How to assign List>Vector3>[] 2 Answers
Storing a list when application Quits 0 Answers
Best way to make spawn points available/unavailable. 1 Answer