【问题标题】:How to create Linked Array List in C#如何在 C# 中创建链接数组列表
【发布时间】:2013-03-11 19:46:07
【问题描述】:

我需要创建一个具有链表容量的数组。

基本上,我需要一个基于静态索引的列表(如数组),但可以获取下一个和上一个字段(并且可以轻松地在列表中循环来回,就像链表一样)。 注意:数组是二维的。我使用自定义类作为数组值。所以我可以为每个实例设置上一个和下一个属性。

是否有为此内置的 C# 集合?如果没有,关于如何创建一个非常简单的版本的任何建议? (我已经有一个版本,由 2 个方法组成。一个向前循环设置前一个字段,一个向后循环设置下一个字段,但它仍然很混乱)。

提前致谢

编辑:

问题是我使用二维数组。如果循环遍历我的数组:

            for (byte x = 0; x < Grid.GetLength(0); x++) 
            {
                for (byte y = 0; y < Grid.GetLength(1); y++) /
                {
                    //At certain point, I need to get the previous field. I can do:
                    if (y != 0)
                    {
                        y -= 2; //-2 because I will y++ in for. Already getting messy
                    }
                    else 
                    {
//What if y == 0? Then I can't do y--. I should get max y and  do x-- to get previous element:

                        y = (byte)(Grid.GetLength(1) - 1); //to get max value y

                        x--;
                    }
}
    }

【问题讨论】:

    标签: c# arrays collections linked-list


    【解决方案1】:

    有一个内置的LinkedList&lt;T&gt; 类。

    但是根据您的描述,为什么数组不起作用?它是静态的,基于索引的,您可以通过递增/递减索引轻松获取下一个和上一个元素。很难从代码中准确看出您需要什么,但我想指出,您可以使用以下方法轻松枚举多维数组:

    var arry = new int[2,3];
    foreach(var item in arry)
    {
        ...
    }
    

    因此,您也许可以将其与 Stack&lt;T&gt; 结构结合起来(将项目推入堆栈并将它们弹出以获取上一个)。

    或者,您可以将数组直接转换为LinkedList

    var list = new LinkedList(arry.Cast<int>()); // flattens array
    

    或者保留原始数组中的索引并仍然以链表的形式循环遍历值:

    var list = new LinkedList(arry.Cast<int>.Select((item, i) => new 
    { 
        Item = item, 
        Index1 = i % arry.GetLength(1), 
        Index2 = i / arry.GetLength(0) 
    }));
    var node = list.First;
    while(node.Next != null)
    {
        Console.WriteLine("Value @ {1}, {2}: {0}", node.Value.Item, node.Value.Index1, node.Value.Index2);
        // on some condition move to previous node
        if (...)
        {
            node = node.Previous;
        }
        else
        {
            node = node.Next;
        }
    }
    

    【讨论】:

    • 因为我使用的是二维数组。我将放置代码 sn-ps 并在我的答案中解释问题,而不是在这里。泰:)
    • 谢谢。看起来我很不擅长解释:(正如代码中所解释的(或至少尝试过),我需要在列表中动态循环来回转发,而我无法轻松做到这一点(参见代码中的 cmets ) 使用二维数组
    • @user1933169 好的,我已经更新了我的答案。我想我已经提供了适合您需求的解决方案。
    【解决方案2】:

    不,你没有。不要放弃传统数组来代替“智能链接节点数组”,这似乎是您的目标,而是尝试在循环体中添加几个变量:

    byte x_len = Grid.GetLength(0);
    byte y_len = Grid.GetLength(1);
    byte prev_x, next_x, prev_y, next_y;
    
    for (byte x = 0; x < x_len; ++x) 
    {
      prev_x = x == 0? x_len - 1 : x - 1;
      next_x = x == x_len - 1? 0 : x + 1;
      for (byte y = 0; y < y_len; ++y)
      {
        prev_y = y == 0? y_len - 1 : y - 1;
        next_y = y == y_len - 1? 0 : y + 1;
    
        // here, you have access to the next and previous
        // in both directions, satisfying your requirements
        // without confusing your loop variables.
    
      }
    }
    

    【讨论】:

    • 这可能是一个很好的答案。但我对你使用的语法很不熟悉。
    • @user1933169 ?:conditional or ternary operator
    • k,会调查的。 Tyvm
    猜你喜欢
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    相关资源
    最近更新 更多