Unity/자료구조
[3.29] Array
ljw4104
2021. 3. 29. 15:35
정의
- 동일한 자료형을 가진 데이터들의 집합
int[] x = new int[5];
- Array는 Index로 각 원소에 접근이 가능하다. ( ∵ Array는 선형 자료구조이고 int같은 형식과 크기가 정해져 있으므로 주소가 계산이 가능하기 때문에)
연산 | 수행시간 |
삽입 | O(1) : 인덱스 연산을 이용한 삽입 O(n) : 삽입 시 원소를 다 밀어버려야 되는 경우 |
삭제 | O(1) : 인덱스 연산을 이용한 삭제 O(n) : 삭제 시 원소를 다 밀어버려야 되는 경우 |
검색 | O(1) : 인덱스 연산을 이용한 검색 O(n) : 특정한 값을 찾는 경우 |
O(x) : Big-O 표기법
빅오 표기법 (big-O notation) 이란
컴퓨터 과학(Computer Science) 에서 알고리즘은 어떠한 문제를 해결하기 위한 방법이고, 어떠한 문제를 해결 하기 위한 방법은 다양하기 때문에 방법(알고리즘) 간에 효율성을 비교하기 위해 빅오(
noahlogs.tistory.com
가변배열
- n차원 배열의 길이가 각각 다른 배열
using System;
namespace Study00
{
public class App
{
public App()
{
int[][] arr = new int[3][];
arr[0] = new int[2];
arr[1] = new int[] { 1, 2, 3, 4, 5, 6 };
arr[2] = new int[] { 7, 8, 9 };
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
Console.Write(arr[i][j] + " ");
}
Console.WriteLine();
}
}
}
}
동적 배열
- 고정된 배열의 크기를 가지는 정적배열이 아닌 런타임시간에 확장기능을 갖는 배열.
using System;
namespace Study00
{
public class App
{
private object[] arr;
public App()
{
arr = new object[0];
Add(5);
Add("Hello");
foreach(object i in arr)
{
Console.WriteLine(i);
}
}
public void Add(object element)
{
var temp = new object[arr.Length + 1];
for(int i = 0; i < arr.Length; i++)
{
temp[i] = arr[i];
}
arr = temp;
arr[arr.Length - 1] = element;
}
}
}