public class StaticArray : MonoBehaviour
{
//자료형 [] : 정적 배열
int[] array1; // 배열 선언
int[] array2 = {10, 20, 30, 40, 50}; // 배열 선언과 초기화
int[] array3 = new int[5]; // 배열 선언 및 공간 할당
int[] array4 = new int[5] { 10, 20, 30, 40, 50 }; // 배열 선언 및 공간 할당 + 초기화
private void Start()
{
array1 = new int[5]; // array1 할당, 코드에서 할당하지 않아도 유니티 인스펙터 상에서 할당 가능
}
}
public class MultidimensionalArray : MonoBehaviour
{
public int[,] array1 = new int[3, 3]; // 3 x 3 => 2차원 배열
public int[,,] array2 = new int [3, 3, 3]; // 3차원 배열
}
public class JaggedArray : MonoBehaviour
{
public int[] array1 = new int[3];
public int[][] jaggedArray1 = new int[3][];
private void Start()
{
array1[0] = 1;
array1[1] = 2;
array1[2] = 3;
jaggedArray1[0] = new int[3] { 1, 2, 3 };
jaggedArray1[1] = new int[2] { 4, 5 };
jaggedArray1[2] = new int[5] { 6, 7, 8, 9, 10 };
}
}
public class String : MonoBehaviour
{
public string str1 = " Hello World ";
public string[] str2 = new string[] { "Hello", "Unity", "World" };
private void Start()
{
Debug.Log(str1[0]); // H
Debug.Log(str1[1]); // l
Debug.Log(str2[0]); // Hello
Debug.Log(str2[2]); // World
Debug.Log(str1.Length); // 문자열 길이 : 13
Debug.Log(str1.Trim()); // 앞뒤 공백 제거 : Hello World
Debug.Log(str1.Trim('l')); // 문자 'l' 제거 : Heo Word
Debug.Log(str1.Contains('H')); // 대문자 H가 있는지
Debug.Log(str1.Contains('h')); // 소문자 h가 있는지
Debug.Log(str1.Contains("Hello")); // Hello가 있는지
Debug.Log(str1.ToUpper()); ; // 대문자 변환
Debug.Log(str1.ToLower()); ; // 소문자 변환
string text = "Apple,Banana,Orange,Melon,WaterMelon,Mango";
string[] fruits = text.Split(','); // 특정 문자로 쪼개기
foreach (var fruit in fruits)
Debug.Log(fruit);
}
}
public class DynamicArray : MonoBehaviour
{
object[] array = new object[3];
void Add(object o)
{
var temp = new object[array.Length + 1];
for (int i = 0; i < array.Length; i++)
{
temp[i] = array[i];
}
array = temp;
array[array.Length -1] = o;
}
}
public class DynamicArray : MonoBehaviour
{
public List<int> list1 = new List<int>();
public List<int> list2 = new List<int>() { 1, 2, 3, 4 };
}
public class DynamicArray : MonoBehaviour
{
public List<int> list1 = new List<int> ();
private void Start()
{
for (int i = 1; i <= 10; i++)
{
list1.Add(i); // 뒤에 i를 추가
}
/*
list1.Insert(5, 100); // 인덱스 5에 100을 삽입
list1.Remove(5); // 값 5를 제거
list1.RemoveAt(5); // 인덱스 5번에 있는 값을 제거
list1.RemoveRange(1, 3); // 인덱스 1번에서 3까지 제거
list1.Clear(); // 데이터를 모두 제거
*/
}
}
public class StudyStack : MonoBehaviour
{
public Stack<int> stack = new Stack<int>();
void Start()
{
for (int i = 1; i <= 10; i++)
{
stack.Push(i); // 1 ~ 10까지 값을 Stack에 추가
}
Debug.Log(stack.Pop()); // Last 값을 뽑음
Debug.Log(stack.Peek()); // 그 다음에 뽑힐 대상을 확인
}
}
public class StudyQueue : MonoBehaviour
{
public Queue<int> queue = new Queue<int>();
void Start()
{
for (int i = 1; i <= 10; i++)
{
queue.Enqueue(i); // 1 ~ 10까지 추가
}
int output = queue.Dequeue(); // 값을 뽑음
Debug.Log(output);
Debug.Log(queue.Peek()); // 그 다음에 뽑을 값을 확인
Debug.Log(queue.Contains(5)); // 값 5가 포함되어있는지 확인
queue.Clear(); // 모든 값 삭제
Debug.Log(queue.Count); // 개수 확인
}
}
public class StudyDictionary : MonoBehaviour
{
public Dictionary<string, int> persons = new Dictionary<string, int>();
void Start()
{
// Dictionary에 데이터 추가
persons.Add("철수", 10);
persons.Add("영희", 15);
persons.Add("동수", 17);
int age = persons["철수"]; // Key값으로 value를 출력
Debug.Log($"철수의 나이는 {age}입니다.");
foreach (var person in persons)
{
if (person.Value == 15)
Debug.Log($"나이가 15인 사람의 이름은 {person.Key}입니다.");
Debug.Log($"{person.Key}의 나이는 {person.Value}입니다.");
}
}
}

public class StudyGraph : MonoBehaviour
{
private int[,] nodes = new int[6, 6]
{
// 0 1 2 3 4 5
{ 0, 1, 0, 1, 1, 0 }, // 0
{ 1, 0, 1, 0, 0, 0 }, // 1
{ 0, 1, 0, 1, 0, 0 }, // 2
{ 1, 0, 1, 0, 0, 0 }, // 3
{ 1, 0, 0, 0, 0, 1 }, // 4
{ 0, 0, 0, 0, 1, 0 }, // 5
};
}
public class StudyGraph : MonoBehaviour
{
private int[,] nodes = new int[6, 6]
{
// 0 1 2 3 4 5
{ 0, 9, 0, 2, 7, 0 }, // 0
{ 9, 0, 20, 0, 0, 0 }, // 1
{ 0, 20, 0, 16, 0, 0 }, // 2
{ 2, 0, 16, 0, 0, 0 }, // 3
{ 7, 0, 0, 0, 0, 10 }, // 4
{ 0, 0, 0, 0, 10, 0 }, // 5
};
}

public class TreeNode<T>
{
public T Data;
public List<TreeNode<T>> Children;
public TreeNode(T data)
{
Data = data;
Children = new List<TreeNode<T>>();
}
}
