移动表列表

本文关键字:列表 移动 | 更新日期: 2023-09-27 18:32:08

例如,我有这个矩阵:

var source = new[]
    {
        new[] { "r", "b", "g", "y" },
        new[] { "r", "b", "g", "y" },
        new[] { "r", string.Empty, "g", "y" },
        new[] { "r", "b", "g", "y" }
    }

我想先将空字符串移动到顶部位置,以创建等效于

var result = new[]
    {
        new[] { "r", "string.Empty", "g", "y" },
        new[] { "r", "b", "g", "y" },
        new[] { "r", "b", "g", "y" },
        new[] { "r", "b", "g", "y" }
    }

如果需要,我想将空字符串向右移动以创建等效于

var result = new[]
    {
        new[] { "r", "g", "y", string.Empty },
        new[] { "r", "b", "g", "y" },
        new[] { "r", "b", "g", "y" },
        new[] { "r", "b", "g", "y" }
    }

这是我到目前为止的代码

  static void Moved(List<List<string>> list)
    {
        Console.WriteLine("Elements:");

检查列表

        foreach (var sublist in list)
        {
            string empty = " ";
            list[2][2] = empty;

检查子列表并访问列表中的元素

            for (int i = 0; i < sublist.Count; i++)
            {
                if (sublist[2] == empty)
                {
                    sublist[2] = sublist[3];
                }
            }

子列表中的值

            foreach (var value in sublist)
            {
                Console.Write(value);
                Console.Write(' ');
            }
            Console.WriteLine();
        }
    }

移动表列表

这是另一个解决方案:

class Program
{
    static void Main(string[] args)
    {
        var source = new[,]
    {
        { "r", "b", "g", "y" },
        { "r", "b", "g", "y" },
        { "r", string.Empty, "g", "y" },
        { "r", "b", "g", "y" }
    };
        PrintArray(source);
        Move(source, new pos() { row = 2, col = 1 }, new pos() { row = 0, col = 3 });
        Console.ReadKey();
    }
    static void Move(string[,] arr, pos from, pos to)
    {
        MoveV(arr, from, to);
        PrintArray(arr);
        MoveH(arr, from, to);
        PrintArray(arr);
    }
    // Moves an item verticaly.
    static void MoveV(string[,] arr, pos from, pos to)
    {
        // Gets the distance to move.
        int delta = to.row - from.row;
        // Gets the direction of the movement (+ or -)
        int mov = Math.Sign(delta);
        // Moves an item.
        for (int row = from.row, i = 0; i < Math.Abs(delta); row += mov, i++)
        {
            Swap(arr, new pos() { row = row, col = from.col }, new pos() { row = row + mov, col = from.col });
        }
    }
    // Moves an item horizonataly.
    static void MoveH(string[,] arr, pos from, pos to)
    {
        int delta = to.col - from.col;
        int mov = Math.Sign(delta);
        for (int col = from.col, i = 0; i < Math.Abs(delta); col += mov, i++)
        {
            Swap(arr, new pos() { row = to.row, col = col }, new pos() { row = to.row, col = col + mov });
        }
    }
    // Swaps two items on each move.
    static void Swap(string[,] arr, pos from, pos to)
    {
        string val = arr[to.row, to.col];
        arr[to.row, to.col] = arr[from.row, from.col];
        arr[from.row, from.col] = val;
    }
    // Print the array to the console.
    static void PrintArray(string[,] arr)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++)
        {
            sb.AppendLine(string.Format("{0} {1} {2} {3}", getval(arr, i, 0), getval(arr, i, 1), getval(arr, i, 2), getval(arr, i, 3)));
        }
        sb.AppendLine("");
        Console.Write(sb.ToString());
    }
    static string getval(string[,] arr, int row, int col)
    {
        return string.IsNullOrWhiteSpace(arr[row, col]) ? " " : arr[row, col];
    }
}
struct pos
{
    public int row;
    public int col;
}

编辑:

更正 Move 方法中的 bug(硬编码自/到)。