This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ArraySample2 | |
{ | |
class MainApp | |
{ | |
static void Main(string[] args) | |
{ | |
int[] scores = new int[5]; | |
scores[0] = 80; | |
scores[1] = 74; | |
scores[2] = 81; | |
scores[^2] = 90; // 배열의 마지막-1 | |
scores[^1] = 34; // 배열의 마지막 | |
foreach (int score in scores) | |
Console.WriteLine(score); | |
int sum = 0; | |
foreach (int score in scores) | |
sum += score; | |
int average = sum / scores.Length; | |
Console.WriteLine($"Average Score : {average}"); | |
} | |
} | |
} | |
-------------------------------------------------------------------- | |
using System; | |
namespace InitializingArray | |
{ | |
class MainApp | |
{ | |
static void Main(string[] args) | |
{ | |
string[] array1 = new string[3]{ "안녕", "Hello", "Halo" }; | |
// 배열 초기화1 | |
Console.WriteLine("array1..."); | |
foreach (string greeting in array1) | |
Console.WriteLine($" {greeting}"); | |
string[] array2 = new string[] { "안녕", "Hello", "Halo" }; | |
// 배열 초기화2 | |
Console.WriteLine("\narray2..."); | |
foreach (string greeting in array2) | |
Console.WriteLine($" {greeting}"); | |
string[] array3 = { "안녕", "Hello", "Halo" }; | |
// 배열 초기화3 | |
Console.WriteLine("\narray3..."); | |
foreach (string greeting in array3) | |
Console.WriteLine($" {greeting}"); | |
} | |
} | |
} | |
------------------------------------------------------------------------------- | |
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 (int score in scores) | |
Console.Write($"{score} "); | |
Console.WriteLine(); | |
// Array의 주요 메서드와 프로퍼티 | |
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); | |
scores[index] = 61; | |
Console.WriteLine($"Everyone passed ? : " + | |
$"{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(); | |
int[] sliced = new int[3]; | |
Array.Copy(scores, 0, sliced, 0, 3); | |
Array.ForEach<int>(sliced, new Action<int>(Print)); | |
Console.WriteLine(); | |
} | |
} | |
} | |
--------------------------------------------------------------------------- | |
using System; | |
namespace Slice | |
{ | |
class MainApp | |
{ | |
static void PrintArray(System.Array array) | |
{ | |
foreach (var e in array) | |
Console.Write(e); | |
Console.WriteLine(); | |
} | |
static void Main(string[] args) | |
{ | |
char[] array = new char['Z'-'A'+1]; | |
for (int i = 0; i < array.Length; i++) | |
array[i] = (char)('A'+i); | |
PrintArray(array[..]); // 0번째부터 마지막까지 | |
// 인덱싱 기법 중 ..은 슬라이싱 | |
PrintArray(array[5..]); // 5번째부터 끝까지 | |
Range range_5_10 = 5..10; | |
PrintArray(array[range_5_10]); // 5번째부터 9(10-1)번째까지 | |
Index last = ^0; | |
Range range_5_last = 5..last; | |
PrintArray(array[range_5_last]); // 5번째부터 끝(^)까지 | |
PrintArray(array[^4..^1]); // 끝에서 4번째부터 끝(^)에서 1번째까지 | |
} | |
} | |
} |
반응형
'Programming > C# - Window' 카테고리의 다른 글
C#/ 일반화(generic), 인덱서(indexer)k (0) | 2023.06.28 |
---|---|
C#/ 컬렉션(collection)·[ArrayList, Queue, Stack, Hashtable]k (0) | 2023.06.28 |
C#/ 프로퍼티(property)·get/set k (0) | 2023.06.28 |
C#/ 추상클래스(abstract class) (0) | 2023.06.28 |
C#/ 인터페이스(interface) (0) | 2023.06.28 |