ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C#] Array 클래스의 주요 메소드 및 프로퍼티, 예제
    C# 2023. 6. 23. 14:52
    분류 이름 설명
    정적 메소드 Sort() 배열을 정렬합니다.
    BinarySearch<T>() 이진 탐색을 수행합니다.
    IndexOf() 배열에서 찾고자 하는 특정 데이터의 인덱스를 반환합니다.
    TrueForAll<T>() 배열의 모든 요소가 지정한 조건에 부합하는지의 여부를 반환합니다.
    FindIndex<T>() 배열에서 지정한 조건에 부합하는 첫 번째 요소의 인덱스를 반환합니다.
    IndexOf() 메소드가 특정 값을 찾는데 비해, FindIndex<T>() 메소드는 지정한 조건에 바탕하여 값을 찾습니다.
    Resize<T>() 배열의 크기를 재조정 합니다.
    Clear() 배열의 모든 요소를 초기화합니다.
    배열이 숫자 형식 기반이면 0으로, 논리 형식 기반이면 false로, 참조 형식 기반이면
    null로 초기화합니다.
    ForEach<T>() 배열의 모든 요소에 대해 동일한 작업을 수행하게 합니다.
    인스턴스
    메소드
    GetLength() 배열에서 지정한 차원의 길이를 반환합니다.
    프로퍼티 Length 배열의 길이를 반환합니다.
    Rank 배열의 차원을 반환합니다.
    <T>는 형식 배개 변수 (Type Parameter) 라고 함
    메소드를 호출할 때 T 대식 배열의 기반 자료형을 매개 변수로 입력하면 컴파일러가 해당 형식에 맞춰 동작하도록 메소드를 컴파일함
    /*
    2023/06/23 // Array 클래스의 주요 메소드와 프로퍼티 예제
    
    실행 결과
    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
     */
    namespace MoreOnArray
    {
        internal class Program
        {
            private static bool CheckPassed(int score)
            {
                if (score >= 60)
                    return true;
                else
                    return false;
            }
    
            private static void Print (int value)
            {
                Console.Write($"{value} ");
            }
    
            static void Main(string[] args)
            {
                int[] scores = new int[] { 80, 74, 81, 90, 34 };
    
                // 배열 출력
                foreach (int 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 {0}",
                    Array.BinarySearch<int>(scores, 81));
                // 일반 탐색?
                Console.WriteLine("Linear Search : 90 is at {0}",
                    Array.IndexOf(scores, 90));
    
                // 조건 부합 확인, 조건 => 60 이상 인가?
                Console.WriteLine("Everyone passed ? : {0}",
                    Array.TrueForAll<int>(scores, CheckPassed));
    
                // 조건 부합 후 해당 인덱스 리턴
                int index = Array.FindIndex<int>(scores,
                    delegate (int score)
                    {
                        if (score < 60) return true;
                        else return false;
                    });
    
                scores[index] = 61; // 해당 인덱스 값 변경
    
                Console.WriteLine("Everyone passed ? : {0}",
                    Array.TrueForAll<int>(scores, CheckPassed));
    
                Console.WriteLine($"Old length of scores : {scores.GetLength(0)}");
    
                // 배열 길이 재정의
                Array.Resize<int>(ref scores, 10);
    
                Console.WriteLine($"New length of scores : {scores.Length}");
    
                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();
            }
        }
    }

    댓글