Yang.공부방

Array Class (배열클래스)

배열생성 및 조작, 검색, 정렬하여 모든배열의 기본클래스 역할을 수행하도록 메서드를 제공


Array [배열] 

: 동일한 유형의 여러 변수를 배열 데이터구조에 저장가능

: 기본값은 참조타입 : null, 숫자타입 : 0 이다

: 배열의 형식은 추상기본유형 Array에서 파생참조형식이다. (IEnumerable 및 lEbumerable <T> 를 구현하므로 foreach반복문을 사용할수있다)


상속 : Object -> Array


[1차원 배열 예시 int형] 

1
2
int[] arrayName = new int[5];
//int[] arrayName(배열클래스 인스턴스를 담는다) = int형 배열클래스의 인스턴스를 생성 (기본값 : 0)
cs
(int 형이므로 기본값은 0으로 초기화)


[1차원 배열 예시 string형] 

1
2
string[] arrayName5 = new string[5];
//string형 배열클래스 인스턴스를 생성 (기본값 : null)
cs


[1차원 배열 또다른 사용법] 

1
2
int[] arrayName = { 12345 };
// ** 배열클래스의 인스턴스를 arrayName3 에 담는다.
cs
(이렇게 코딩해도 위와 동일하게 작성이 된다.)


[1차원 배열 초기화] 

1
2
int[] arrayName2 = new int[5] { 12345 };
// 출력 값 : 1,2,3,4,5 (여기서 배열의 인덱스는 0,1,2,3,4 이다.
cs
(인덱스는 0으로 시작한다.)


[배열특성의 메소드]

(이미지 참조 : https://m.blog.naver.com/sonicheroes1/220947806856)

<T> 가 붙은 메소드는 형식매개변수(Type Parameter)라고 부르며, 호출 시 T의 위치에 배열기반자료형을 매개변수로 입력하면 컴파일러가 알아서 처리해준다.


[foreach 예시]

1
2
3
4
5
int[] sortEx = { 1,2,3,4,5,6,7,8,9,10 };
foreach(int foreachEx in sortEx)
{
Console.WriteLine(foreachEx);
} //출력값 1 2 3 4 5 6 7 8 9 10
cs

인자로 들어온 ArrayEx 내부의 인덱스 끝까지 순환해주는 반복문, 배열에서 많이 사용

사용법 : foreach(자료형 element in 배열명)

참조 : https://blockdmask.tistory.com/313


[Sort 예시]

1
2
3
4
5
6
7
            int[] sortEx = new int[5] { 541021 };
            Array.Sort(sortEx);
 
            for(int i=0; i<5; i++)
            {
                Console.WriteLine(sortEx[i]);
            }//출력값 : 1 2 4 5 10

cs
(작은 수 -> 큰 수로 정렬이 되어 출력이 된다)

참조 : https://mongyang.tistory.com/86

[BinarySearch<T> 예시]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ArrayEx = { 12,23,34,45,56,67,78,89,91,10 };
            string[] ArrayEx2 = { "가""나""다""라""마" };
 
            Console.WriteLine(Array.BinarySearch<int>(ArrayEx, 89));
            Console.WriteLine(Array.BinarySearch<string>(ArrayEx2, "라"));
        }
    }
}결과 값 : 7 , 3

cs

정렬된 Array에서 값을 검색

참조 : https://docs.microsoft.com/ko-kr/dotnet/api/system.array.binarysearch?view=netframework-4.7.2










1차원배열 doc : https://docs.microsoft.com/ko-kr/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays

Array Method 종류 : https://m.blog.naver.com/sonicheroes1/220947806856

GetValue Method doc : https://docs.microsoft.com/ko-kr/dotnet/api/system.reflection.propertyinfo.getvalue?view=netframework-4.7.2

string.IndexOf Method doc : https://docs.microsoft.com/ko-kr/dotnet/api/system.string.indexof?view=netframework-4.7.2