Machineboy空

C# 컬렉션 - 배열, 다차원 배열 - rank, getLength, Array.IndexOf(), Array.Sort() 본문

언어/C#

C# 컬렉션 - 배열, 다차원 배열 - rank, getLength, Array.IndexOf(), Array.Sort()

안녕도라 2025. 1. 15. 17:29

컬렉션(Collection)이란?

data structures that can hold zero or more elements are known as collections

0개 혹은 더 많은 요소를 가질 수 있는 데이터 구조


배열(Array) 이란 ? + 특징

컬렉션 중 하나

  • fixed size, elements must all be of the same type (모든 요소가 같은 타입, 고정된 크기의 공간)
  • retrieved from using an index start from 0 (0부터 시작하는 인덱스로 검색 가능)
  • reference type
// size가 2인 배열 선언
int[] twoInts = new int[2];

// 요소에 값 할당
twoInts[1] = 8;

// 인덱스로 검색
twoInts[1] == 8; 	// true

 

 

https://machineboy0.tistory.com/76

 

Value vs Reference type

Call by Reference Call by Value 원본 복사 복사본 복사 사물함에 넣어두고 사물함 번호를 알려주는 방법 ref, out을 활용 매개변수에서의 gameObject: (gameObject가 만약 value type이라면 계속 복사해서 쓰니까

machineboy0.tistory.com

https://machineboy0.tistory.com/117

 

값에 의한 호출 (call by value) vs 참조에 의한 호출(call by reference)

함수에 값을 전달 할 때 두 가지 방법 Call by Value(값에 의한 호출) Call by Reference(참조에 의한 호출) 매개변수로 전달되는 변수를 모두 함수 내부에서 복사해서 함수 실행 함수 내부에서 전체 복사

machineboy0.tistory.com


배열(Array) 선언

// 3 가지 같은 방식

int[] threeIntsV1 = new int[] {4, 9, 7};
int[] threeIntsV2 = new[] {4, 9, 7};
int[] threeIntsV3 = {4, 9, 7}
// 1차원 배열(single-dimensional array)

int[] array1 = new int[5];		// {0, 0, 0, 0, 0}
int[] array2 = [1,2,3,4,5,6];	// C#12 이후
int[] array3 = {1,2,3,4,5,6};	// C#12 이전


// 2차원 배열(two dimensional array)

int[,] multiDimensionalArray1 = new int[2,3];  // { { 0, 0, 0 }, { 0, 0, 0 } }
int[,] multiDimensionalArray2 = {{1 ,2, 3}, {4, 5, 6}};


// 배열의 배열, 가변 배열(jagged array)

int[][] jaggedArray = new int[6][];
jaggedArray[0] = [1, 2, 3, 4];


배열(Array) 요소 에 접근

// 1차원 배열(single-dimensional array) : index로 접근

string[] weekDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
weekDays[0]	// Sun


// 다차원 배열(multi-dimensional array) : index, getLength(), Rank = 차원

// 2차원 배열
int[,] array2D = {{1,2},{3,4},{5,6},{7,8}};
                                
array2D[0,0] = 1;
array2D[1,0] = 3;
array2D[3,0] = 7;

// 3차원 배열
int[,,] array3D = { { { 1, 2, 3 }, { 4,   5,  6 } },
                                { { 7, 8, 9 }, { 10, 11, 12 } } };
                                
array3D[1,0,1] = 8;
array3D[1,1,2] = 12;

array2D.Rank = 2	// Rank는 차원을 의미
array3D.Rank = 3

var allLength = array3D.Length;
var total = 1;

for(int i = 0; i < array3D.Rank; i++)
{
	total *= array3D.GetLength(i);
}

allLength = 12;
total = 12;

// 가변 배열, 배열의 배열(jaggedArray)

int[][] jaggedArray = new int[3][];

jaggedArray[0] = [1,3,5,7,9];
jaggedArray[1] = [0,2,4,6];
jaggedArray[2] = [11,22];

int[][] jaggedArray2 = 
[
	[1,3,5,7,9],
    [0,2,4,6],
    [11,22]
];


int[][,] jaggedArray3 = 
[
	new int[,] {{1,3},{5,7}},
    new int[,] {{0,2},{4,6},{8,10}},
    new int[,] {{11,22},{99,88},{0,9}}
]

jaggedArray3[0][1,0];	//5
jaggedArray3.Length;	//3


배열(Array)  함수 1 : Array.IndexOf(Array, Object)

using System;

public static class ResistorColor
{
    public static int ColorCode(string color) => Array.IndexOf(Colors(), color);

    public static string[] Colors() => new[] {"black","brown","red","orange","yellow","green","blue","violet","grey","white"};
}

배열(Array)  함수 2: Array.Sort(Array)

int[] input = new int[]{3,1,4,2,9};

Array.Sort(input);