본문 바로가기
KDT/C# 프로그래밍

23/07/25 다차원 배열 기초연습

by 잰쟁 2023. 7. 25.
728x90
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace starcraft
{
    internal class App
    {
        

        //생성자
        public App()
        {
            //정수형 2차원 배열 정의 및 배열의 인스턴스 생성
            int[,] map = new int[3, 2];

            //인덱스로 배열의 요소에 접근
            map[2, 1] = 1;

            //배열의 순회 (요소를 출력)
            int rowLength = map.GetLength(0);
            int colLength = map.GetLength(1);


            //맵을 출력
            for(int i = 0; i < rowLength; i++)
            {
                for(int j = 0; j < colLength; j++)
                {
                    Console.Write("[{0},{1}] : {2}\t", i, j, map[i, j]);
                }
                Console.WriteLine();
            }
        }
       
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace starcraft
{
    internal class App
    {
        

        //생성자
        public App()
        {
            int[,] map =
            {
                {1,1,1 },
                {1,1,2 }                
            };

            for(int i = 0; i < map.GetLength(0); i++)
            {
                for(int j =0;j<map.GetLength(1); j++)
                {
                    int element = map[i, j];
                    Console.Write("[{0},{1}] : ({2})\t", i, j, element);
                }
                Console.WriteLine();
            }
        }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace starcraft
{
    internal class App
    {
        

        //생성자
        public App()
        {
            string [,] map =
            {
                {"풀","풀","풀" },
                {"풀","풀","사막" }                
            };

            for(int i = 0; i < map.GetLength(0); i++)
            {
                for(int j =0;j<map.GetLength(1); j++)
                {
                    string element = map[i, j];
                    Console.Write("[{0},{1}] : ({2})\t", i, j, element);
                }
                Console.WriteLine();
            }
        }

    }
}