c# - 列表中的 OutOfMemoryException

标签 c# .net switch-statement out-of-memory element

有人可以帮助我并建议如何解决或处理此 OutOfMemoryException 吗?

addMany 的任务是在索引后添加一个数字序列。例如,如果我们用它来添加 10、20 和 30,从包含 1 2 3 4 5 6 7 8 的列表中的第三个位置开始,列表将如下所示: 1 2 3 10 20 30 4 5 6 7 8.

它只抛出这个输入:

1 2 3 4 5 6 7 8

push 9

push 21

pop

shift

addMany 3 10 20 30

remove 5

print

这是我的代码:

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));

        }
    }
}

最佳答案

addMany 3 10 20 30

这是导致异常的命令。 首先,当你做这个任务时

arr = copy;

您没有复制 by value but by reference ,就像一对 List 经常发生的那样。

换句话说,您没有两个具有相同值的不同“容器”,而是两个指向相同值“容器”的“指针”。

如果您对 arr 进行更改,您也会更改 copy ,反之亦然,因为它们实际上指向相同的值“容器”。

所以,当你进入这个循环时:

    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]);
        }
    }

然后你添加一些东西来复制,就像这里

copy.Add(arr[i]);

你正在主动添加到arr,递增arr.Count,这是你的for循环的退出条件。

所以你永远不会离开你的 for 循环,因为对于每次迭代......你也向上移动了退出条件。

而且,您知道,无限循环会填满您的内存,导致抛出异常。

只是为了添加更多细节,如果你想将一个 List 复制到另一个,你需要一个所谓的 deep copy, while so far you have a shallow copy .

在您的情况下,列表之间的深层复制就像 adding 一样简单ToList():

arr = copy.ToList();

请注意,这并不总是安全的;在您的情况下它有效,因为您的列表包含整数,即 value types .如果您想完全确定并避免意外,在您的 for 循环计算退出条件之前,将其保存在一个整数中并将其用作退出条件:

int ExitCount = arr.Count();
for (int i = 0; i < ExitCount; i++)

关于c# - 列表中的 OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49219006/

相关文章:

c# - 连接或添加字符串但不使用 string.Format 时出现内存碎片?

javascript - 单击时的 CSS 列表垂直菜单

java - switch 语句的 Jacoco 覆盖率

c# - Linq 到 XML 转换器

mysql - 将 MySQLGuid(来自 Devart dotConnect for MySQL)转换为 SQL 中的字符串

c# - 了解 Ninject 的好处

.net - 了解 .net 中已编译的正则表达式

您可以在 C 语言的 switch 语句中的 case 中使用 while 循环吗?

c# - 在不公开类的情况下在项目之间共享 C# 中的代码

c# - 试图获得身份中的所有角色