C# 陣列反轉 Array Reverse

static void Main(string[] args)
{
    int[,] arr = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
    mPrint(arr);
    Console.WriteLine();

    int[,] arrReverse = mFun(arr);
    mPrint(arrReverse);

    Console.Read();
}

/// <summary>
/// 列印陣列
/// </summary>        
static void mPrint(int[,] arrIn)
{
    for (int i = 0; i < arrIn.GetLength(0); i++)
    {
        for (int j = 0; j < arrIn.GetLength(1); j++)
            Console.Write(arrIn[i, j]);
        Console.WriteLine();
    }
}

/// <summary>
/// 反轉陣列
/// </summary>
static int[,] mFun(int[,] arrIn)
{
    int[,] arrOut = new int[arrIn.GetLength(1), arrIn.GetLength(0)];
    for (int i = 0; i < arrOut.GetLength(0); i++)
    {
        for (int j = 0; j < arrOut.GetLength(1); j++)
            arrOut[i, j] = arrIn[j, i];
    }

    return arrOut;
}

發佈留言