C#/์ด๊ฒƒ์ด C#์ด๋‹ค

50. 2์ฐจ์› ๋ฐฐ์—ด

Rainbow๐ŸŒˆCoder 2022. 2. 24. 19:06
728x90

2์ฐจ์› ๋ฐฐ์—ด์€ ๊ฐœ๋…์ ์œผ๋กœ 2๊ฐœ์˜ ์ฐจ์›(์„ธ๋กœ+๊ฐ€๋กœ)์œผ๋กœ ์›์†Œ๋ฅผ ๋ฐฐ์น˜ํ•œ๋‹ค๊ณ ๋„ ๋งํ•˜๊ณ ,

1์ฐจ์› ๋ฐฐ์—ด์„ ์›์†Œ๋กœ ๊ฐ–๋Š” ๋ฐฐ์—ด์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค.

 

2์ฐจ์› ๋ฐฐ์—ด์˜ ์„ ์–ธ

๋ฐ์ดํ„ฐํ˜•์‹[,] ๋ฐฐ์—ด์ด๋ฆ„ = new ๋ฐ์ดํ„ฐํ˜•์‹[2์ฐจ์›๊ธธ์ด, 1์ฐจ์›๊ธธ์ด];

 

1์ฐจ์›์˜ ๊ธธ์ด๋Š” ๊ฐ€๋กœ ๋ฐฉํ–ฅ,

2์ฐจ์›์˜ ๊ธธ์ด๋Š” ์„ธ๋กœ ๋ฐฉํ–ฅ.

            int[,] test = new int[2, 3];
            test[0, 0] = 1;
            test[0, 1] = 2;
            test[0, 2] = 3;
            test[1, 0] = 4;
            test[1, 1] = 5;
            test[1, 2] = 6;

int[2,3]์€ ๊ธธ์ด๊ฐ€ 3์ธ 1์ฐจ์› ๋ฐฐ์—ด์„ ์›์†Œ๋กœ 2๊ฐœ ๊ฐ–๊ณ  ์žˆ๋Š” 2์ฐจ์› ๋ฐฐ์—ด์ด๋ผ๊ณ  ์ฝ๋Š”๋‹ค.

(2์ฐจ์› ๋ฐฐ์—ด์„ ์ฝ”๋“œ์—์„œ ์ฝ์„ ๋•Œ๋Š” [] ์•ˆ์— ์žˆ๋Š” ์ฐจ์›์˜ ๊ธธ์ด๋ฅผ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ฝ์œผ๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋‹ค.)

 

<2์ฐจ์› ๋ฐฐ์—ด์˜ ์„ ์–ธ>

            int[,] test1 = new int[2, 3] { { 1,2,3},{4,5,6 } }; //๋ฐฐ์—ด์˜ ํ˜•์‹๊ณผ ๊ธธ์ด ๋ช…์‹œ
            int[,] test2 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };//๋ฐฐ์—ด์˜ ๊ธธ์ด ์ƒ๋žต
            int[,] test3 = { { 1, 2, 3 }, { 4, 5, 6 } };//ํ˜•์‹๊ณผ ๊ธธ์ด ๋ชจ๋‘ ์ƒ๋žต

<์˜ˆ์ œ>

using System;

namespace MoreOnArray
{
    class MainApp
    {

        static void Main(string[] args)
        {
            int[,] test1 = new int[2, 3] { { 1,2,3},{4,5,6 } };
            for(int i=0; i<test1.GetLength(0); i++)
            {
                for(int j=0; j<test1.GetLength(1); j++)
                {
                    Console.Write($"[{i},{j}]:[{test1[i,j]}]");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            int[,] test2 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
            for (int i = 0; i < test2.GetLength(0); i++)
            {
                for (int j = 0; j < test2.GetLength(1); j++)
                {
                    Console.Write($"[{i},{j}]:[{test2[i, j]}]");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            int[,] test3 = { { 1, 2, 3 }, { 4, 5, 6 } };
            for (int i = 0; i < test3.GetLength(0); i++)
            {
                for (int j = 0; j < test3.GetLength(1); j++)
                {
                    Console.Write($"[{i},{j}]:[{test3[i, j]}]");
                }
                Console.WriteLine();
            }
        }
    }
}

์ถœ๋ ฅ๊ฒฐ๊ณผ

[0,0]:[1][0,1]:[2][0,2]:[3]
[1,0]:[4][1,1]:[5][1,2]:[6]

[0,0]:[1][0,1]:[2][0,2]:[3]
[1,0]:[4][1,1]:[5][1,2]:[6]

[0,0]:[1][0,1]:[2][0,2]:[3]
[1,0]:[4][1,1]:[5][1,2]:[6]
728x90