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
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
50. 2์ฐจ์ ๋ฐฐ์ด (0) | 2022.02.24 |
---|---|
49. Array๋ก ์ํ๋ฒณ ์ถ๋ ฅํ๋ ์์ (0) | 2022.02.24 |
47. ๋ฐฐ์ด์ฐ๋ ์ด์ , ๋ฐฐ์ด ์ด๊ธฐํ ๋ฐฉ๋ฒ 3๊ฐ์ง (0) | 2022.02.24 |
46. ์ฑํฐ9 ์ฐ์ต๋ฌธ์ (0) | 2022.02.24 |
45. ์ถ์ํด๋์ค์ ํ๋กํผํฐ (0) | 2022.02.24 |