๋ฐ˜์‘ํ˜•

๐Ÿ“˜ ๋ฐฐ์—ด๊ณผ ์ปฌ๋ ‰์…˜


๋ฐฐ์—ด

1์ฐจ์› ๋ฐฐ์—ด

๋™์ผํ•œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ์—ฐ์†์ ์œผ๋กœ ์ €์žฅํ•˜๋ฉฐ, ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐ์—ด์€ ์„ ์–ธ ์‹œ ๊ณ ์ •๋œ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋ฉฐ, ํฌ๊ธฐ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ๋Š” ์ €์žฅํ•  ์ˆ˜ ์—†๋‹ค.

int[] array1 = new int[5];
array1[0] = 1;
...
...

์˜ˆ์ œ: ๋ฐฐ์—ด์„ ํ™œ์šฉํ•œ ํ•ฉ๊ณ„ ๊ณ„์‚ฐ

int[] itemPrices = { 100, 200, 300, 400, 500 };
int totalPrice = 0;
for (int i = 0; i < itemPrices.Length; i++)
{
    totalPrice += itemPrices[i];
}
Console.WriteLine("์ด ์•„์ดํ…œ ๊ฐ€๊ฒฉ: " + totalPrice + " gold");

๋ฐฐ์—ด ์‹ค์Šต ์˜ˆ์‹œ

  • ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ ๋Šฅ๋ ฅ์น˜ ๋ฐฐ์—ด
  • ํ•™์ƒ ์„ฑ์  ํ‰๊ท  ๊ณ„์‚ฐ
  • ๋ฐฐ์—ด ๊ธฐ๋ฐ˜ ์ˆซ์ž ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„

๋‹ค์ฐจ์› ๋ฐฐ์—ด

ํ–‰๊ณผ ์—ด๋กœ ๊ตฌ์„ฑ๋œ 2์ฐจ์› ์ด์ƒ์˜ ๋ฐฐ์—ด ๊ตฌ์กฐ๋กœ, ํ‘œ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

int[,] array2D = new int[2, 3];
int[,,] array3D = new int[2, 3, 4];

์˜ˆ์ œ: 2์ฐจ์› ๋ฐฐ์—ด์„ ํ™œ์šฉํ•œ ๊ฒŒ์ž„ ๋งต

int[,] map = {
    {1,1,1,1,1},
    {1,0,0,0,1},
    ...
};

์ปฌ๋ ‰์…˜

์ปฌ๋ ‰์…˜์€ ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ, ํฌ๊ธฐ๊ฐ€ ๊ฐ€๋ณ€์ ์ด๋ฉฐ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ๋‹ค. C#์˜ ์ปฌ๋ ‰์…˜์€ System.Collections.Generic ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์ •์˜๋˜์–ด ์žˆ๋‹ค.

List

๋™์ ์œผ๋กœ ํฌ๊ธฐ๊ฐ€ ์กฐ์ •๋˜๋Š” ๋ฐฐ์—ด ๊ตฌ์กฐ๋กœ, ์š”์†Œ์˜ ์ถ”๊ฐ€/์‚ญ์ œ/์‚ฝ์ž…์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฐฐ์—ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•˜๋ฉฐ, ์š”์†Œ๊ฐ€ ๋งŽ์•„์งˆ ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ™•์žฅ๋œ๋‹ค.

List<int> numbers = new List<int>();
numbers.Add(1); numbers.Remove(1);

Dictionary

ํ‚ค์™€ ๊ฐ’์˜ ์Œ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ตฌ์กฐ๋‹ค. ํ‚ค๋Š” ์ค‘๋ณต๋  ์ˆ˜ ์—†์œผ๋ฉฐ, ๊ฐ’์— ๋น ๋ฅด๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

Dictionary<string, int> scores = new Dictionary<string, int>();
scores.Add("Alice", 100);

Stack

ํ›„์ž…์„ ์ถœ(LIFO) ๊ตฌ์กฐ๋กœ ๋™์ž‘ํ•˜๋ฉฐ, Push()๋กœ ์ถ”๊ฐ€ํ•˜๊ณ  Pop()์œผ๋กœ ์ œ๊ฑฐํ•œ๋‹ค.

Stack<int> stack = new Stack<int>();
stack.Push(1); int val = stack.Pop();

Queue

์„ ์ž…์„ ์ถœ(FIFO) ๊ตฌ์กฐ๋กœ ๋™์ž‘ํ•˜๋ฉฐ, Enqueue()๋กœ ์ถ”๊ฐ€ํ•˜๊ณ  Dequeue()๋กœ ์ œ๊ฑฐํ•œ๋‹ค.

Queue<int> queue = new Queue<int>();
queue.Enqueue(1); int val = queue.Dequeue();

HashSet

์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ์ง‘ํ•ฉ ๊ตฌ์กฐ๋‹ค. ์‚ฝ์ž… ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ๋˜์ง€ ์•Š์œผ๋ฉฐ, Contains()๋กœ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

HashSet<int> set = new HashSet<int>();
set.Add(1); set.Contains(1);

๋ฐฐ์—ด vs ๋ฆฌ์ŠคํŠธ

ํ•ญ๋ชฉ ๋ฐฐ์—ด ๋ฆฌ์ŠคํŠธ
ํฌ๊ธฐ ๊ณ ์ • ๊ฐ€๋ณ€
๋ฉ”๋ชจ๋ฆฌ ์ •์  ํ• ๋‹น ๋™์  ๋ฐฐ์—ด ๊ตฌ์กฐ
๊ธฐ๋Šฅ ๋‹จ์ˆœ ์ €์žฅ/์ ‘๊ทผ ์‚ฝ์ž…, ์‚ญ์ œ, ํƒ์ƒ‰ ๋“ฑ ๋‹ค์–‘
์„ฑ๋Šฅ ๋ฐ˜๋ณต์— ์œ ๋ฆฌ ์œ ์—ฐ์„ฑ ๋›ฐ์–ด๋‚จ
 
  • ๋ฐ์ดํ„ฐ ํฌ๊ธฐ๊ฐ€ ๊ณ ์ •๋˜๊ฑฐ๋‚˜ ๋ฐ˜๋ณต ์—ฐ์‚ฐ ์ค‘์‹ฌ์ผ ๊ฒฝ์šฐ ๋ฐฐ์—ด ์‚ฌ์šฉ์ด ์œ ๋ฆฌํ•˜๋‹ค.
  • ์œ ๋™์ ์ธ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, ์‚ฝ์ž…/์‚ญ์ œ๊ฐ€ ์žฆ์€ ๊ฒฝ์šฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ด๋‹ค.

 

๊ฒŒ์ž„์ผ€๋ฆญํ„ฐ ๋Šฅ๋ ฅ์น˜ ๋งŒ๋“ค๊ธฐ

// ํ”Œ๋ ˆ์ด์–ด์˜ ๊ณต๊ฒฉ๋ ฅ, ๋ฐฉ์–ด๋ ฅ, ์ฒด๋ ฅ, ์Šคํ”ผ๋“œ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
int[] playerStats = new int[4]; 

// ๋Šฅ๋ ฅ์น˜๋ฅผ ๋žœ๋ค์œผ๋กœ ์ƒ์„ฑํ•˜์—ฌ ๋ฐฐ์—ด์— ์ €์žฅ
Random rand = new Random();
for (int i = 0; i < playerStats.Length; i++)
{
    playerStats[i] = rand.Next(1, 11);
}

// ๋Šฅ๋ ฅ์น˜ ์ถœ๋ ฅ
Console.WriteLine("ํ”Œ๋ ˆ์ด์–ด์˜ ๊ณต๊ฒฉ๋ ฅ: "  + playerStats[0]);
Console.WriteLine("ํ”Œ๋ ˆ์ด์–ด์˜ ๋ฐฉ์–ด๋ ฅ: "  + playerStats[1]);
Console.WriteLine("ํ”Œ๋ ˆ์ด์–ด์˜ ์ฒด๋ ฅ: "    + playerStats[2]);
Console.WriteLine("ํ”Œ๋ ˆ์ด์–ด์˜ ์Šคํ”ผ๋“œ: "  + playerStats[3]);

 

ํ•™์ƒ๋“ค์˜ ์„ฑ์  ํ‰๊ท  ๊ตฌํ•˜๊ธฐ

int[] scores = new int[5];  // 5๋ช…์˜ ํ•™์ƒ ์„ฑ์ ์„ ์ €์žฅํ•  ๋ฐฐ์—ด

// ์„ฑ์  ์ž…๋ ฅ ๋ฐ›๊ธฐ
for (int i = 0; i < scores.Length; i++)
{
    Console.Write("ํ•™์ƒ " + (i + 1) + "์˜ ์„ฑ์ ์„ ์ž…๋ ฅํ•˜์„ธ์š”: ");
    scores[i] = int.Parse(Console.ReadLine());
}

// ์„ฑ์  ์ดํ•ฉ ๊ณ„์‚ฐ
int sum = 0;
for (int i = 0; i < scores.Length; i++)
{
    sum += scores[i];
}

// ์„ฑ์  ํ‰๊ท  ์ถœ๋ ฅ
double average = (double)sum / scores.Length;
Console.WriteLine("์„ฑ์  ํ‰๊ท ์€ " + average + "์ž…๋‹ˆ๋‹ค.");

 

๋ฐฐ์—ด์„ ํ™œ์šฉํ•œ ์ˆซ์ž ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„

Random random = new Random();  // ๋žœ๋ค ๊ฐ์ฒด ์ƒ์„ฑ
int[] numbers = new int[3];  // 3๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด

// 3๊ฐœ์˜ ๋žœ๋ค ์ˆซ์ž ์ƒ์„ฑํ•˜์—ฌ ๋ฐฐ์—ด์— ์ €์žฅ
for (int i = 0; i < numbers.Length; i++)
{
    numbers[i] = random.Next(1, 10);
}

int attempt = 0;  // ์‹œ๋„ ํšŸ์ˆ˜ ์ดˆ๊ธฐํ™”
while (true)
{
    Console.Write("3๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” (1~9): ");
    int[] guesses = new int[3];  // ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ˆซ์ž๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด

    // ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ˆซ์ž ๋ฐฐ์—ด์— ์ €์žฅ
    for (int i = 0; i < guesses.Length; i++)
    {
        guesses[i] = int.Parse(Console.ReadLine());
    }

    int correct = 0;  // ๋งž์ถ˜ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜ ์ดˆ๊ธฐํ™”

    // ์ˆซ์ž ๋น„๊ต ๋ฐ ๋งž์ถ˜ ๊ฐœ์ˆ˜ ๊ณ„์‚ฐ
    for (int i = 0; i < numbers.Length; i++)
    {
        for (int j = 0; j < guesses.Length; j++)
        {
            if (numbers[i] == guesses[j])
            {
                correct++;
                break;
            }
        }
    }

    attempt++;  // ์‹œ๋„ ํšŸ์ˆ˜ ์ฆ๊ฐ€
    Console.WriteLine("์‹œ๋„ #" + attempt + ": " + correct + "๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋งž์ถ”์…จ์Šต๋‹ˆ๋‹ค.");

    // ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๋งž์ถ˜ ๊ฒฝ์šฐ ๊ฒŒ์ž„ ์ข…๋ฃŒ
    if (correct == 3)
    {
        Console.WriteLine("์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค! ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๋งž์ถ”์…จ์Šต๋‹ˆ๋‹ค.");
        break;
    }
}

 

๊ฒŒ์ž„ ๋งต์„ ๊ตฌํ˜„

int[,] map = new int[5, 5] 
{ 
    { 1, 1, 1, 1, 1 }, 
    { 1, 0, 0, 0, 1 }, 
    { 1, 0, 1, 0, 1 }, 
    { 1, 0, 0, 0, 1 }, 
    { 1, 1, 1, 1, 1 } 
};

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 5; j++)
    {
        if (map[i, j] == 1)
        {
            Console.Write("โ–  ");
        }
        else
        {
            Console.Write("โ–ก ");
        }
    }
    Console.WriteLine();
}

 

๋ฐ˜์‘ํ˜•