An issue with a nested list.

My TestScript:

public static List<List<int>> listA = new List<List<int>>();
public static List<int> listB = new List<int>();

public static void TestMethod()
{
    listB.Clear();
    // a code that adds ints to the list b
    //

    listA.Add(listB);
       
        foreach (List<int> subList in listA)
        {
            Debug.Log("A sublist in list A: ");
            for (int i = 0; i < subList.Count; i++)
            {
                Debug.Log(subList[i]);
            }
        }
}

What i’m trying to do:
I’m trying to add listB to a list of lists listA. I’m calling the TestMethod() to add some ints to listB after which i add the list to the listA. The list gets cleared on calling the method, but right after it gets populated again with new values that i add to the listB. I expect the foreach statement to list every list that i have added to the listA and every list to be different from another, as the code that adds the ints ensures that the values are going to be different.

What occurs:
The foreach statement seems to display one list multiple times. Upon re-calling the method it displays a different list, but again, multiple times.

What i think might be the issue:
The static keyword, however in my project i have lists with the static keyword that don’t exhibit the same issues that i have with the nested list, so i’m not sure.

Any help is appreciated!

All of your listB’s are the same listB. So you end up with X references to the same listB inside listA, where X is the number of times you call TestMethod. What it sounds like you want is to add a copy of listB to listA instead of just adding listB, or instead of clearing listB you create a new listB. Doing either would result in listA having a bunch of different lists, instead of references to the exact same list over and over.

Why you’ve made this mistake might be a misunderstanding of value types vs reference types. Lists are reference types. There’s a lot of good explanations on the interwebs you could look up if that’s the issue.

1 Like

Thank you for your quick answer, @Joe-Censored ! So to avoid any confusion: all i need to do is to create new lists out of listB each time the code adds a bunch of values to a listB and then add those new lists to a nested list listA, correct?

1 Like

Try this code instead, and see if it works for you:

public static List<List<int>> listA = new List<List<int>>();

public static void TestMethod()
{
    List<int> listB = new List<int>();
    // a code that adds ints to the list b
    //

    listA.Add(listB);
      
        foreach (List<int> subList in listA)
        {
            Debug.Log("A sublist in list A: ");
            for (int i = 0; i < subList.Count; i++)
            {
                Debug.Log(subList[i]);
            }
        }
}

Notice I’ve removed the static declaration for listB, and instead make it a local list inside the TestMethod method.

1 Like

Yes, it seems to do what i wanted it to do. Thank you for your help again, @Joe-Censored ! While i think i understand the issue now, i assume that there is no solution for the problem if i wanted to keep the listB static, correct?

Sure there is:

public static List<List<int>> listA = new List<List<int>>();
public static List<int> listB = new List<int>();

public static void TestMethod()
{
    listB = new List<int>();
    // a code that adds ints to the list b
    //

    listA.Add(listB);
   
        foreach (List<int> subList in listA)
        {
            Debug.Log("A sublist in list A: ");
            for (int i = 0; i < subList.Count; i++)
            {
                Debug.Log(subList[i]);
            }
        }
}

But why the hell do you want to make that list static? It looks like you want to erase or replace it each time you use it anyway. But maybe you just have other code somewhere else which needs to access it, I don’t know.

1 Like

Seems like you guessed it right! :slight_smile: Thank you for your help, i’ve been trying to solve it out for quite a while. I started troubleshooting starting not with the very core of the issue (e.g the method that adds the lists and the exact way it does that) but with the “end” of the method calling chain (the result that i expected to get using the nested list). Lesson learned, problem solved.

1 Like