I was wondering how you would copy multidimensional lists so that one could be altered and the other not.
I want to do something like this:
public class MultidimensionalLists : MonoBehaviour
{
private List<List<int>> list1;
private List<List<int>> list2;
private void Start()
{
list1 = new List<List<int>> { new List<int> { 0, 1, 2, 3 } };
list2 = new List<List<int>> { new List<int> { 3, 2, 1, 0 } };
list2.Clear();
list2.AddRange(list1);//set list2 to list1
//print list2[0], prints 0 1 2 3
string result = "";
foreach (var item in list2[0])
{
result += item.ToString() + " ";
}
Debug.Log(result);
list1[0].Clear();//clear list1[0]
//print list2[0], prints a cleared list (nothing)
result = "";
foreach (var item in list2[0])
{
result += item.ToString() + " ";
}
Debug.Log(result);
}
}
The problem is that when I clear list1[0], list2[0] clears as well.