본문 바로가기
C#

다차원 배열

by Mostlove 2023. 7. 9.
728x90
반응형

2차원 배열

namespace CSharp1
{
    internal class Program
    {
        class Map
        {
            int[,] tiles =
            {
                { 1, 1, 1, 1, 1 },
                { 1, 0, 0, 0, 1 },
                { 1, 0, 0, 0, 1 },
                { 1, 0, 0, 0, 1 },
                { 1, 1, 1, 1, 1 },
            };
            public void Render()
            {
                var defaultColor = Console.ForegroundColor;
                for (int i = 0; i < tiles.GetLength(1); i++)
                {
                    for (int j = 0; j < tiles.GetLength(0); j++)
                    {
                        if (tiles[j, i] == 1)
                            Console.ForegroundColor = ConsoleColor.Red;
                        else
                            Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write("●");
                    }
                    Console.WriteLine();
                }
                Console.ForegroundColor=defaultColor;
            }
        }

        static void Main(string[] args)
        {
            Map map = new Map();
            map.Render();
        }
    }
}

가변 배열

            int[][] a = new int[2][];
            a[0] = new int[3];
            a[1] = new int[6];
            a[2] = new int[2];

            a[0][0] = 1

반응형

'C#' 카테고리의 다른 글

Dictionary  (0) 2023.07.09
List  (0) 2023.07.09
배열 연습문제  (0) 2023.07.06
배열  (0) 2023.07.06
TextRPG(객체지향 버전)  (0) 2023.07.06