My code:
Stack<int>[] stk = new Stack<int>[5];
stk[2].Push(54);
stk[1].Push(768);
Error:
System.NullReferenceException: 'Object reference not set to an instance of an object.
The debugger shows that I have 5 stacks initialized to null values. How can I initialize those 5 stacks so I can push values into them?
I also tried:
List<Stack<int>> data = new List<Stack<int>>( );
then created stacks a, b, c, etc and pushed them into data with data.Add(a)
etc.
and this works fine ( data[1].Push(345)
) but the number stacks I need to store varies and I want to be able to iterate through the stacks or reference them individually.
Or is there a better, hopefully easier, way to do this with arrays maybe?