728x90
Unity Array List Dictonary
1. Array
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Test : MonoBehaviour
{
// 배열 선언 방법
// 데이터형식[] 배열이름 = new 데이터형식[크기]
public Item[] items = new Item[]
{
new Item(3, "carot"),
new Item(2, "banana"),
new Item(1, "apple"),
new Item(4, "apple")
};
void Start()
{
Array.Sort(items, (x,y) => x.code.CompareTo(y.code));
Array.ForEach(items, x => x.Print());
bool b = Array.Exists(items, x => x.code == 3);
print(b);
bool b2 = Array.TrueForAll(items, x => x.code > 0);
print(b2);
Item item = Array.Find(items, x => x.name == "apple");
item.Print();
int index = Array.FindIndex(items, x => x.name == "apple");
print(index);
item = Array.FindLast(items, x => x.name == "apple");
item.Print();
index = Array.FindLastIndex(items, x => x.name == "apple");
print(index);
Item[] _items = Array.FindAll(items, x => x.name == "apple");
Array.ForEach(_items, x => x.Print());
// Item[] items_copy = items; // 얕은 복사
Item[] items_copy = Array.ConvertAll(items, x => new Item(x.code, x.name)); // 깊은 복사
items_copy[0].code = 100;
Array.ForEach(items, x => x.Print());
Array.ForEach(items_copy, x => x.Print());
Array.Reverse(items);
Array.Resize(ref items, 5);
}
}
[Serializable]
public class Item
{
public int code;
public string name;
public Item(int code, string name)
{
this.code = code;
this.name = name;
}
public void Print()
{
Debug.Log($"code:{code}, name:{name}");
}
}
2. List
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Test : MonoBehaviour
{
// List 선언 방법
// List<자료형> 변수명 = new List<자료형>();
[SerializeField] List<Item> items = new List<Item>()
{
new Item(3, "carrot"),
new Item(2, "banana"),
new Item(1, "apple"),
new Item(4, "apple")
};
void Start()
{
for(int i=0; i<items.Count; i++)
{
items[i].Print();
}
foreach(var item in items)
{
item.Print();
}
Item newitem = new Item(5, "watermelon");
items.Add(newitem);
bool b = items.Remove(newitem);
print(b);
items.RemoveAt(1);
int count = items.RemoveAll(x => x.code > 2);
print(count);
items.Insert(1, new Item(5, "watermelon"));
items.Sort((x,y) => x.code.CompareTo(y.code));
bool b2 = items.Exists(x => x.name == "carrot");
print(b2);
bool b3 = items.TrueForAll(x => x.code > 0);
print(b3);
//List<Item> itemscopy = items; // 얕은 복사
List<Item> itemscopy = items.ConvertAll(x => new Item(x.code, x.name)); // 깊은 복사
itemscopy[0].code = 100;
itemscopy.ForEach(x => x.Print());
Item itemf = items.Find(x => x.name == "apple");
itemf.Print();
int indexf = items.FindIndex(x => x.name == "apple");
print(indexf);
itemf2 = items.FindLast(x => x.name == "apple");
itemf2.Print();
List<Item> items2 = items.FindAll(x => x.code > 2);
items2.ForEach(x => x.Print());
Item[] itemArray = items.ToArray(); // List -> Array
List<Item> lst = new List<Item>(itemArray); // Array -> List
Array.ForEach(itemArray, x => x.Print());
}
}
[Serializable]
public class Item
{
public int code;
public string name;
public Item(int code, string name)
{
this.code = code;
this.name = name;
}
public void Print()
{
Debug.Log($"code:{code}, name:{name}");
}
}
3. Dictionary
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Test : MonoBehaviour
{
Dictionary<string, Item> items = new Dictionary<string, Item>()
{
{"banana", new Item(2, "banana")},
{"apple", new Item(1, "apple")}
};
void Start()
{
foreach(var keyValue in items)
{
print(keyValue.Key);
keyValue.Value.Print();
}
bool b = items.ContainsKey("apple");
print(b);
Item _item = new Item(3, "carrot");
items.Add("carrot", _item);
bool b2 = items.ContainsValue(_item);
print(b2);
if(items.TryGetValue("apple", out Item _item2))
{
_item2.Print();
}
}
}
[Serializable]
public class Item
{
public int code;
public string name;
public Item(int code, string name)
{
this.code = code;
this.name = name;
}
public void Print()
{
Debug.Log($"code:{code}, name:{name}");
}
}
728x90
'3D > Unity' 카테고리의 다른 글
Unity 마우스커서 변경 (0) | 2022.10.24 |
---|---|
Unity ColorUtility (0) | 2022.10.11 |
Action 으로 다른 스크립트 함수 가져오는 방법 (0) | 2022.10.10 |
Unity에서 Json을 사용하는 방법 (0) | 2022.10.09 |
코루틴 (0) | 2022.10.09 |