Rainbow๐ŸŒˆCoder 2022. 2. 24. 15:22
728x90

C#์—์„œ๋Š” ๋ชจ๋“  ๊ฒƒ์ด ๊ฐ์ฒด์ด๋‹ค.

๋ฐฐ์—ด๋„ ๊ฐ์ฒด์ด๋ฉฐ,

๋ฐฐ์—ด์€ System.Array ํด๋ž˜์Šค์— ๋Œ€์‘๋œ๋‹ค.

 

๋‹ค์Œ ์˜ˆ์ œ๋Š” int ๊ธฐ๋ฐ˜์˜ ๋ฐฐ์—ด์ด System.Array ํ˜•์‹์—์„œ ํŒŒ์ƒ๋˜์—ˆ์Œ์„ ๋ณด์—ฌ์ค€๋‹ค.

using System;

namespace DerivedFromArray
{
    class MainApp
    {
        static void Main(string[] args)
        {
            int[] array = new int[] { 10, 30, 20, 7, 1 };
            Console.WriteLine($"Type of array : {array.GetType()}");
            Console.WriteLine($"Base type of array : {array.GetType().BaseType}");
        }
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ

Type of array : System.Int32[]
Base type of array : System.Array

๋”ฐ๋ผ์„œ System.Array์˜ ํŠน์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ๋ฅผ ํŒŒ์•…ํ•˜๋ฉด ๋ฐฐ์—ด์˜ ํŠน์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ๋ฅผ ์•Œ๊ฒŒ๋˜๋Š” ์…ˆ์ด๋‹ค.

๋ณด๋„ˆ์Šค๋กœ ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์žฌ๋ฏธ์žˆ๋Š” ์ผ๋“ค๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋ฐฐ์—ด์˜ ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๋ฅผ ์›ํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•œ๋‹ค๋“ ๊ฐ€, ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฐ์—ด ์†์—์„œ ์ฐพ์•„๋‚ด๋Š” ์ž‘์—…๋“ค์„!

 

<Array ํด๋ž˜์Šค์˜ ์ฃผ์š” ๋ฉ”์†Œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋“ค>

๋ถ„๋ฅ˜ ์ด๋ฆ„ ์„ค๋ช…
์ •์  ๋ฉ”์†Œ๋“œ Sort() ๋ฐฐ์—ด์„ ์ •๋ ฌ
BinarySearch<T>() ์ด์ง„ํƒ์ƒ‰ ์ˆ˜ํ–‰
IndexOf() ๋ฐฐ์—ด์—์„œ ์ฐพ๊ณ ์ž ํ•˜๋Š” ํŠน์ • ๋ฐ์ดํ„ฐ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
TrueForAll<T>() ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ง€์ •ํ•œ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š”์ง€์˜ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜
FindIndex<T>() ๋ฐฐ์—ด์—์„œ ์ง€์ •ํ•œ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜. IndexOf() ๋ฉ”์†Œ๋“œ๊ฐ€ ํŠน์ • ๊ฐ’์„ ์ฐพ๋Š”๋ฐ ๋น„ํ•ด, FindIndex<T>() ๋ฉ”์†Œ๋“œ๋Š” ์ง€์ •ํ•œ ์กฐ๊ฑด์— ๋ฐ”ํƒ•ํ•˜์—ฌ ๊ฐ’์„ ์ฐพ์Œ
Resize<T>() ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ์žฌ์กฐ์ •
Clear() ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ดˆ๊ธฐํ™”. ๋ฐฐ์—ด์ด ์ˆซ์ž ํ˜•์‹ ๊ธฐ๋ฐ˜์ด๋ฉด 0์œผ๋กœ, ๋…ผ๋ฆฌ ํ˜•์‹ ๊ธฐ๋ฐ˜์ด๋ฉด false๋กœ, ์ฐธ์กฐ ํ˜•์‹ ๊ธฐ๋ฐ˜์ด๋ฉด null๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
ForEach<T>() ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ์— ๋Œ€ํ•ด ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ํ•œ๋‹ค
Copy<T>() ๋ฐฐ์—ด์˜ ์ผ๋ถ€๋ฅผ ๋‹ค๋ฅธ ๋ฐฐ์—ด์— ๋ณต์‚ฌ
์ธ์Šคํ„ด์Šค
๋ฉ”์†Œ๋“œ
GetLength() ๋ฐฐ์—ด์—์„œ ์ง€์ •ํ•œ ์ฐจ์›์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜(๋‹ค์ฐจ์› ๋ฐฐ์—ด์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋จ!)
ํ”„๋กœํผํ‹ฐ Length ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜
Rank ๋ฐฐ์—ด์˜ ์ฐจ์›์„ ๋ฐ˜ํ™˜

๋’ค์— <T>๋ฅผ ๋ถ™์ด๊ณ  ๋‹ค๋‹ˆ๋Š” ๋ฉ”์†Œ๋“œ๋“ค์—์„œ <T>๋Š” ํ˜•์‹ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ผ๊ณ  ํ•˜๋Š”๋ฐ,

์ด๋“ค ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ T ๋Œ€์‹  ๋ฐฐ์—ด์˜ ๊ธฐ๋ฐ˜ ์ž๋ฃŒํ˜•์„ ์ธ์ˆ˜๋กœ ์ž…๋ ฅํ•ด์ฃผ๋ฉด

์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํ•ด๋‹น ํ˜•์‹์— ๋งž์ถฐ ๋™์ž‘ํ•˜๊ฒŒ๋” ๋ฉ”์†Œ๋“œ๋ฅผ ์ปดํŒŒ์ผํ•œ๋‹ค.(์ผ๋ฐ˜ํ™”)

 

<Array ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ํ™œ์šฉํ•˜๋Š” ์˜ˆ์ œ>

using System;

namespace MoreOnArray
{

    class MainApp
    {
        private static bool CheckPassed(int score)
        {
            return score >= 60;
        }
        private static void Print(int value)
        {
            Console.Write($"์ถœ๋ ฅ๊ฐ’์€ {value} ");
        }
        static void Main(string[] args)
        {
            int[] scores = new int[] { 80, 74, 81, 90, 34 };
            foreach (var score in scores)
                Console.Write($"{score} ");
            Console.WriteLine();

            Array.Sort(scores);
            Array.ForEach<int>(scores, new Action<int>(Print));
            Console.WriteLine();

            Console.WriteLine($"Number of dimensions : {scores.Rank}");

            Console.WriteLine($"Binary Search : 81 is at + {Array.BinarySearch<int>(scores, 81)}");
            Console.WriteLine($"Linear Search : 90 is at + {Array.IndexOf(scores,90)}");
            Console.WriteLine($"Everyone passed ? : " + $"{Array.TrueForAll<int>(scores, CheckPassed)}");

            int index = Array.FindIndex<int>(scores, (score) => score < 60);
            //FindIndex ๋ฉ”์†Œ๋“œ๋Š” ํŠน์ • ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค.
            //๋žŒ๋‹ค์‹ ํ™œ์šฉ

            scores[index] = 61;
            Console.WriteLine($"Everyone passed ? : " + $"{Array.TrueForAll<int>(scores, CheckPassed)}");
            Console.WriteLine("Old length of scores : "+$"{scores.GetLength(0)}");

            //5์˜€๋˜ ๋ฐฐ์—ด์˜ ์šฉ๋Ÿ‰์„ 10์œผ๋กœ ์žฌ์กฐ์ •
            Array.Resize<int>(ref scores, 10);
            Console.WriteLine($"New length of scores : {scores.Length}");

            //Action ๋Œ€๋ฆฌ์ž ํ™œ์šฉ
            Array.ForEach<int>(scores, new Action<int>(Print));
            Console.WriteLine();

            Array.Clear(scores, 3, 7);
            Array.ForEach<int>(scores, new Action<int>(Print));
            Console.WriteLine();

            int[] sliced = new int[3];
            Array.Copy(scores, 0,sliced,0,3); //scores ๋ฐฐ์—ด์˜ 0๋ฒˆ์งธ๋ถ€ํ„ฐ 3๊ฐœ ์š”์†Œ๋ฅผ sliced ๋ฐฐ์—ด์˜ 0๋ฒˆ์งธ~2๋ฒˆ์งธ ์š”์†Œ์— ์ฐจ๋ก€๋Œ€๋กœ ๋ณต์‚ฌํ•œ๋‹ค.
            Array.ForEach<int>(sliced, new Action<int>(Print));
            Console.WriteLine();
        }
    }
}

<์ถœ๋ ฅ ์ƒํƒœ>

 

80 74 81 90 34
์ถœ๋ ฅ๊ฐ’์€ 34 ์ถœ๋ ฅ๊ฐ’์€ 74 ์ถœ๋ ฅ๊ฐ’์€ 80 ์ถœ๋ ฅ๊ฐ’์€ 81 ์ถœ๋ ฅ๊ฐ’์€ 90
Number of dimensions : 1
Binary Search : 81 is at + 3
Linear Search : 90 is at + 4
Everyone passed ? : False
Everyone passed ? : True
Old length of scores : 5
New length of scores : 10
์ถœ๋ ฅ๊ฐ’์€ 61 ์ถœ๋ ฅ๊ฐ’์€ 74 ์ถœ๋ ฅ๊ฐ’์€ 80 ์ถœ๋ ฅ๊ฐ’์€ 81 ์ถœ๋ ฅ๊ฐ’์€ 90 ์ถœ๋ ฅ๊ฐ’์€ 0 ์ถœ๋ ฅ๊ฐ’์€ 0 ์ถœ๋ ฅ๊ฐ’์€ 0 ์ถœ๋ ฅ๊ฐ’์€ 0 ์ถœ๋ ฅ๊ฐ’์€ 0
์ถœ๋ ฅ๊ฐ’์€ 61 ์ถœ๋ ฅ๊ฐ’์€ 74 ์ถœ๋ ฅ๊ฐ’์€ 80 ์ถœ๋ ฅ๊ฐ’์€ 0 ์ถœ๋ ฅ๊ฐ’์€ 0 ์ถœ๋ ฅ๊ฐ’์€ 0 ์ถœ๋ ฅ๊ฐ’์€ 0 ์ถœ๋ ฅ๊ฐ’์€ 0 ์ถœ๋ ฅ๊ฐ’์€ 0 ์ถœ๋ ฅ๊ฐ’์€ 0
์ถœ๋ ฅ๊ฐ’์€ 61 ์ถœ๋ ฅ๊ฐ’์€ 74 ์ถœ๋ ฅ๊ฐ’์€ 80

 

728x90