移动表格列表

时间:2014-11-27 09:58:32

标签: c#

例如,我有这个矩阵:

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

    }

1 个答案:

答案 0 :(得分:1)

这是另一个解决方案:

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方法中记录一个错误(从/到硬编码)。