티스토리 뷰
1. Array 클래스
C#에서 생성되는 모든 배열은 .NET 프레임워크의 System.Array 클래스를 상속받는다. 이에 따라 모든 배열은
Array 클래스의 모든 멤버를 사용할 수 있다.
1-1. Array 클래스의 주요 메서드와 프로퍼티
1-1-1. 정적 메서드
- BinarySearch() : 이진 탐색을 수행 (Object, Generic 타입)
- Clear() : 배열의 지정한 요소를 기본값으로 초기화
- FindIndex() : 배열에서 지정한 조건에 부합하는 첫 번째 요소의 인덱스를 반환
- ForEach() : 배열의 모든 요소에 대해 동일한 작업을 수행
- IndexOf() : 배열에서 찾고자 하는 특정 데이터의 인덱스를 반환
- Resize() : 배열의 크기를 재조정
- Sort() : 배열을 정렬
- TrueForAll() : ForAll() : 배열의 모든 요소가 지정한 조건에 부합하는지 여부
1-1-2. 인스턴스 메서드
- GetLength() : 배열에서 지정한 차원의 길이를 반환
1-1-3. 프로퍼티
- Length : 배열의 길이
- Rank : 배열의 차원
namespace _09.Array
{
class Program
{
static bool CheckScore(int score)
{
if (score >= 70)
return true;
else
return false;
}
static void Consumer(int value)
{
Console.Write(value + " ");
}
static void Main(string[] args)
{
int[] scores = new int[] { 71, 65, 82, 93, 68 };
foreach (int score in scores)
Console.Write(score + " "); // 71 62 82 93 68
Console.WriteLine();
System.Array.Sort(scores);
System.Array.ForEach<int>(scores, new Action<int>(Consumer)); // 65 68 71 82 93
Console.WriteLine();
Console.WriteLine("배열의차원: "+scores.Rank); // 배열의차원: 1
Console.WriteLine("BSearch : 82 is at {0}", System.Array.BinarySearch(scores, 82));
Console.WriteLine("BSearch : 68 is at {0}", System.Array.BinarySearch(scores, 68));
Console.WriteLine("모든요소70이상? {0}", System.Array.TrueForAll<int>(scores, CheckScore));
int index = System.Array.FindIndex<int>(scores, delegate (int score)
{
if (score > 80)
return true;
else
return false;
});
Console.WriteLine("배열의 요소중 80이상인 첫 번째 요소의 인덱스: {0}", index); // 3
System.Array.Resize<int>(ref scores, 10);
System.Array.ForEach<int>(scores, new Action<int>(Consumer)); // 65 68 71 82 93 0 0 0 0 0
Console.WriteLine();
System.Array.Clear(scores, 0, scores.Length);
System.Array.ForEach<int>(scores, new Action<int>(Consumer)); // 0 0 0 0 0 0 0 0 0 0
Console.WriteLine();
}
}
}
2. 컬렉션
2-1. 주요 컬렉션
- ArrayList, Queue, Stack, Hashtable 등
2-1-1. ArrayList
- 제너릭이 아닌 오브젝트 객체를 매개값으로 받음
- 초기화시 유일하게 초기자로 초기화 가능 // 예) ArrayList list = new ArrayList() { 값1, 값2, 값3 };
2-1-2. Hashtable
- 키와 값의 쌍으로 이루어진 형태
- 딕셔너리 초기자를 이용한 초기화 가능 // 예) { [키1] = 값1, [키2] = 값2] };
3. 인덱서
- 인덱스를 이용해 객체 내 데이터에 접근하게 하는 프로퍼티
3-1. 선언 형식
class 클래스명
{
한정자 인덱서형식 this[형식 식별자]
{
get
{
// index를 이용해 내부 데이터 반환
}
set
{
// index를 이용해 내부 데이터 저장
}
}
}
3-2. 인덱서를 이용한 예제
namespace _02.Indexer
{
class Indexer
{
private int[] arr;
public Indexer()
{
Console.WriteLine("인덱서 생성자 호출");
this.arr = new int[3];
}
public int this[int index]
{
get
{
if (index >= arr.Length)
{
Console.WriteLine("underflow");
return -1;
}
return arr[index];
}
set
{
if (index >= arr.Length)
{
Array.Resize<int>(ref arr, index + 1);
Console.WriteLine("overflow index+1");
}
arr[index] = value;
}
}
}
class Program
{
static void Main(string[] args)
{
Indexer idx = new Indexer();
for (int i = 0; i < 10; i++)
idx[i] = i + 10;
for (int i = 0; i < 10; i++)
Console.Write(idx[i] + " ");
Console.WriteLine();
}
}
}
'프로그래밍 언어 > C#' 카테고리의 다른 글
[C# Basic] 프로퍼티 (0) | 2020.01.05 |
---|---|
[C# Basic] 형변환 (0) | 2020.01.05 |
[C# Basic] 인덱서 (0) | 2020.01.05 |
[C# Basic] 메서드 파라미터 (0) | 2020.01.04 |
[C# Basic] Nullable 타입 (0) | 2020.01.04 |
댓글