Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我该如何解决这个问题进程因StackOverflowException而终止_C#_Stack Overflow_Mergesort - Fatal编程技术网

C# 我该如何解决这个问题进程因StackOverflowException而终止

C# 我该如何解决这个问题进程因StackOverflowException而终止,c#,stack-overflow,mergesort,C#,Stack Overflow,Mergesort,这是我的代码,用于对由名称(string)、价格(int)、速度(int)和权重(int)组成的字符串组成的输入进行排序。看起来像这样: 雅马哈1200 200 300 哈雷999 210 399 戴维森128 288 2888 等等 我必须使用合并排序 static void Main(string[] args) { DataTuple = new List<Tuple<string, int, int, int>>();

这是我的代码,用于对由名称(string)、价格(int)、速度(int)和权重(int)组成的字符串组成的输入进行排序。看起来像这样: 雅马哈1200 200 300 哈雷999 210 399 戴维森128 288 2888 等等

我必须使用合并排序

    static void Main(string[] args)
    {
        DataTuple = new List<Tuple<string, int, int, int>>();
        Start();

    }

    private static void Start()
    {
        aantalopties = int.Parse(Console.ReadLine());
        Tuple<string, int, int, int> tupleTemp;
        invoer = new string[aantalopties];

        for (int n = 0; n < aantalopties; n++)
        {
            NaamEnData = Console.ReadLine();  // read in data
            NED = NaamEnData.Split(' ');      // split data
            naam = NED[0];
            prijs = int.Parse(NED[1]);
            snelheid = int.Parse(NED[2]);
            gewicht = int.Parse(NED[3]);       
            tupleTemp = new Tuple<string, int, int, int>(naam, prijs, snelheid, gewicht); //add data to tuple
            DataTuple.Add(tupleTemp); //add tuple to list
        }

        mergeSort(DataTuple);


        Console.ReadLine();
    }

    private static List<Tuple<string,int,int,int>> mergeSort(List<Tuple<string,int,int,int>> data )
    {

        if (data.Count() < 1) return data;

        List<Tuple<string, int, int, int>> links; //links means left
        links = new List<Tuple<string, int, int, int>>();
        List<Tuple<string, int, int, int>> rechts; //rechts means right
        rechts = new List<Tuple<string, int, int, int>>();

        for(int n =0; n < data.Count(); n++)
        {
            if (n % 2 > 0)
                links.Add(data[n]);
            else
                rechts.Add(data[n]);
        }

        links = mergeSort(links);
        rechts = mergeSort(rechts);            

        return merge(links,rechts);
    }

    private static List<Tuple<string, int, int, int>> merge(List<Tuple<string, int, int, int>> links, List<Tuple<string, int, int, int>> rechts)
    {

        List<Tuple<string, int, int, int>> resultaat;
        resultaat = new List<Tuple<string, int, int, int>>();  //resultaat means result


        while (links.Count > 0 && rechts.Count > 0)
        {
            if (links[0].Item2 <= rechts[0].Item2)
            {
                resultaat.Add(links[0]);
                links.RemoveAt(0);
            }
            else
            {
                resultaat.Add(rechts[0]);
                rechts.RemoveAt(0);
            }
        }

        while(links.Count > 0)
        {
            resultaat.Add(links[0]);
            links.RemoveAt(0);
        }

        while (rechts.Count > 0)
        {
            resultaat.Add(rechts[0]);
            rechts.RemoveAt(0);
        }

        return resultaat;

    }
}
static void Main(字符串[]args)
{
DataTuple=新列表();
Start();
}
私有静态void Start()
{
aantalopties=int.Parse(Console.ReadLine());
Tuple-tupleTemp;
invoer=新字符串[A可能性];
对于(int n=0;n0)
links.Add(数据[n]);
其他的
记录添加(数据[n]);
}
链接=合并排序(链接);
rechts=合并排序(rechts);
返回合并(链接、记录);
}
私有静态列表合并(列表链接、列表记录)
{
列出结果;
resultaat=新列表();//resultaat表示结果
而(links.Count>0&&rechts.Count>0)
{
如果(链接[0].Item2 0)
{
添加结果(链接[0]);
links.RemoveAt(0);
}
而(rechts.Count>0)
{
结果at.Add(rechts[0]);
删除记录(0);
}
返回结果at;
}
}
}

当我试着运行这段代码时,我得到了一个错误“进程由于StackOverflowException而终止”,我认为这与我尝试调用mergesort函数的方式有关。我已经看过了,但是我自己无法解决这个问题。有人能解释一下我做错了什么吗


非常感谢。

此错误通常发生,因为您有递归调用,但没有可以终止它的基本情况。也许可以尝试使用较小的输入大小进行测试,并通过调试器运行,以检查应到达基本情况的位置,但如果使用长度为1的数组调用
mergeSort(data)
,则不检查会发生什么情况。。。您将进入(理论上)无限递归。实际上,您将得到一个
StackOverflow
异常。只有当要传递的数据至少有两个元素时,才能执行递归调用。或者,如果数据长度
<2
(而不是
<1
)需要,则必须打断
mergeSort
的第一行。这与您的问题无关,但如果您坚持在类上使用元组来保存数据(我认为有三个以上的属性是一个很好的理由),则应该使用ValueTuple和元组语法。。。Ie
(字符串,int,int,int)
列表
。。。它不太冗长,而且在很多情况下更容易在视觉上摸索。更不用说你可以命名这些项目,例如
List