Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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# Can';t追查“的原因”;指数超出范围”;例外_C#_List - Fatal编程技术网

C# Can';t追查“的原因”;指数超出范围”;例外

C# Can';t追查“的原因”;指数超出范围”;例外,c#,list,C#,List,我有一个弗兰肯斯坦: var unsorted = new List<(Hand, List<Card>)>(); 我收到了错误的消息 索引超出范围。必须为非负数且小于集合的大小。(参数“索引”) 如果我事先不知道将存储多少项,如何正确初始化此列表 应征求意见: _tempList = IsStraightFlush(theValueHand); if (_tempList.Count == 5) {

我有一个弗兰肯斯坦:

var unsorted = new List<(Hand, List<Card>)>();
我收到了错误的消息

索引超出范围。必须为非负数且小于集合的大小。(参数“索引”)

如果我事先不知道将存储多少项,如何正确初始化此列表

应征求意见:

_tempList = IsStraightFlush(theValueHand);
            if (_tempList.Count == 5)
            {
                unsorted.Add((hand, _tempList));
                continue;
            }
我从中向元组添加列表的函数

        private List<Card> IsStraightFlush(List<Card> hList)
        {
        var st = 0;
        _tempList.Clear();


        foreach (var t in hList)
        {
           ...
        }
        if (_tempList.Count < 5)
        {
            return new List<Card>();
        }

       hList = _tempList.ToList();
        _tempList.Clear();

        for (var i = 0; i < hList.Count - 2; i++)
        {
           ...
            if (st == 4)
            {
                _tempList.Add(hList[i + 1]);
                return _tempList;
            }
        }

        st = 0;
        _tempList.Clear();
        if (hList[0].Value == 13) //Ace through 5
            for (int i = hList.Count - 1, j = 0; i > 0; i--, j++)
            {
              ...
                if (st == 4)
                {
                    _tempList.Add(hList.First());
                    return _tempList.OrderBy(x => x.Value).ToList(); 
                }
            }
        return new List<Card>();
        }

private List SortStraightFlushes(List hList)
{
var sortedHList=hList.OrderBy(x=>x.Item2[0].Value).ToList();
列表输出=新列表();
for(int i=0;i

我不知道它有多漂亮,有多准确,但它的工作原理是这样的:

unsorted.Add((hand, new List<Card>().Concat(_tempList).ToList()));
unsorted.Add((hand,new List().Concat(_templast.ToList()));

您在该行收到该错误?该异常与元组用法无关。向我们展示您的更多代码。@IanKemp在这里您执行了在
unsorted.Add中调用的
Add
方法((hand,_templast))是否有一个名为“index”的参数?如果不是,那就不是抛出异常的那一行。异常stacktrace应显示准确的行号。这很可能是您在其他地方进行的索引器调用之一
resultHands.AddRange(SortStraightFlushes(unsorted));
        private List<(int, Hand)> SortStraightFlushes(List<(Hand, List<Card>)> hList)
    {
        var sortedHList = hList.OrderBy(x => x.Item2[0].Value).ToList();
        List<(int, Hand)> output = new List<(int, Hand)>();
        for (int i = 0; i < sortedHList.Count; i++)
        {
            if(i != sortedHList.Count - 1)
                if (sortedHList[i].Item2[0].Value == sortedHList[i + 1].Item2[0].Value)
                    output.Add((1, sortedHList[i].Item1));
                else
                    output.Add((0, sortedHList[i].Item1));
            else
                output.Add((0, sortedHList[i].Item1));
        }
        return output;
    }
unsorted.Add((hand, new List<Card>().Concat(_tempList).ToList()));