Can someone help me and suggest how to resolve or handle this OutOfMemoryException
?
The task for addMany
is to add a sequence of numbers after the index. For example, if we use it to add 10, 20 and 30, starting from the third position in a list containing 1 2 3 4 5 6 7 8, the list will look like this: 1 2 3 10 20 30 4 5 6 7 8.
It throws only with this input:
1 2 3 4 5 6 7 8
push 9
push 21
pop
shift
addMany 3 10 20 30
remove 5
print
This is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2comandi
{
class Program
{
static void Main(string[] args)
{
List<int> arr = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
List<int> copy = new List<int>();
List<int> copy1 = new List<int>();
string[] commands = Console.ReadLine().Split(' ').ToArray();
string command = commands[0];
while (command != "print") {
switch (command)
{
case "push":
arr.Add(int.Parse(commands[1]));
break;
case "pop":
Console.WriteLine(arr[arr.Count-1]);
arr.Remove(arr[arr.Count-1]);
break;
case "shift":
copy.Add(arr.Count);
for (int i = 0; i < arr.Count - 1; i++)
{
copy.Add(arr[i]);
}
arr = copy;
break;
case "addMany":
int command1 = int.Parse(commands[1]);
if (command1 <= arr.Count)
{
for (int i = 0; i < arr.Count; i++)
{
if (command1 == i)
{
for(int j = 2; j<commands.Length; j++)
{
copy.Add(int.Parse(commands[j]));
}
copy.Add(arr[i]);
}
else
{
copy.Add(arr[i]);
}
}
}
arr = copy;
break;
case "remove":
int command11 = int.Parse(commands[1]);
if (command11 <= arr.Count)
{
arr.Remove(arr[command11]);
}
break;
case "print":break;
}
commands = Console.ReadLine().Split(' ').ToArray();
command = commands[0];
}
arr.Reverse();
Console.WriteLine(String.Join(", ", arr));
}
}
}