I'm learning to program in C#, and I'm running into a problem: I can't seem to call the elements that I've previously stored in a nested List, like so:
private List<List<float>> Order = new List<List<float>>();
private List<float> SubList = new List<float>();
private List<float> Conditions = new List<float>();
string tmp;
string[] levels;
string[] sublevels;
float curlevel;
tmp = "1,2,3;1,3,2;2,1,3;2,3,1;3,1,2;3,2,1"
levels = tmp.Split(';');
for(int i=0;i<levels.Length;i++){
sublevels=levels[i].Split(',');
for(int a=0;a<sublevels.Length;a++){
float.TryParse(sublevels[a], out curlevel);
SubList.Add(curlevel);
}
Order.Add(SubList);
SubList.Clear();
}
foreach(float cond in Order[3]){
Conditions.Add(cond);
}
Although printing the lists' counts shows that my Order list now has 6 items, and each SubList list has 3 items in it, I am not able to call the items in the sublists by indexing the Order list, and therefore my Conditions list, which I want to use in my project, stays empty. Is there another way to index items within a nested list? I also tried:
Conditions[0]=Order[3][0];
Conditions[1]=Order[3][1];
Conditions[2]=Order[3][2];
Which also didn't work. If anyone knows how to do this, I would be very grateful to have your input!
Change you code to
List<List<float>> Order = new List<List<float>>();
//List<float> SubList = new List<float>();
List<float> Conditions = new List<float>();
string tmp;
string[] levels;
string[] sublevels;
float curlevel;
tmp = "1,2,3;1,3,2;2,1,3;2,3,1;3,1,2;3,2,1";
levels = tmp.Split(';');
for (int i = 0; i < levels.Length; i++)
{
List<float> SubList = new List<float>();
sublevels = levels[i].Split(',');
for (int a = 0; a < sublevels.Length; a++)
{
float.TryParse(sublevels[a], out curlevel);
SubList.Add(curlevel);
}
Order.Add(SubList);
//SubList.Clear();
}
foreach (float cond in Order[3])
{
Conditions.Add(cond);
}