Array[배열] #미완성 : 메소드예시
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 |
[1차원 배열 예시 string형]
1 2 | string[] arrayName5 = new string[5]; //string형 배열클래스 인스턴스를 생성 (기본값 : null) | cs |
[1차원 배열 또다른 사용법]
1 2 | int[] arrayName = { 1, 2, 3, 4, 5 }; // ** 배열클래스의 인스턴스를 arrayName3 에 담는다. | cs |
[1차원 배열 초기화]
1 2 | int[] arrayName2 = new int[5] { 1, 2, 3, 4, 5 }; // 출력 값 : 1,2,3,4,5 (여기서 배열의 인덱스는 0,1,2,3,4 이다. | cs |
[배열특성의 메소드]
(이미지 참조 : https://m.blog.naver.com/sonicheroes1/220947806856)
<T> 가 붙은 메소드는 형식매개변수(Type Parameter)라고 부르며, 호출 시 T의 위치에 배열기반자료형을 매개변수로 입력하면 컴파일러가 알아서 처리해준다.
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 배열명)
1 2 3 4 5 6 7 | int[] sortEx = new int[5] { 5, 4, 10, 2, 1 }; Array.Sort(sortEx); for(int i=0; i<5; i++) { Console.WriteLine(sortEx[i]); }//출력값 : 1 2 4 5 10 | cs |
[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
'C# > 미완성' 카테고리의 다른 글
미완성 LOL캐릭터 생성 후 데미지 입기(데이터 저장 및 불러오기) (0) | 2019.04.10 |
---|---|
(미완성)인벤토리 만들기 (0) | 2019.04.08 |
#미완성#인벤토리 (0) | 2019.03.30 |
와우 캐릭터생성(미완성) (0) | 2019.03.27 |
미완성(3.25 과제) new, class, dot(.), 형식변환, 멤버변수, 지역변수 (0) | 2019.03.26 |