Array 예시입니다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Test : MonoBehaviour
{
[SerializeField]
Item[] items = new 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.Length; i++)
items[i].Print();
foreach (Item item in items)
item.Print();
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 b = Array.TrueForAll(items, x => x.code > 0);
print(b);
}
{
Item item = Array.Find(items, x => x.name == "apple");
item.Print();
}
{
int index = Array.FindIndex(items, x => x.name == "apple");
print(index);
}
{
Item item = Array.FindLast(items, x => x.name == "apple");
item.Print();
}
{
int 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 = items; // 얕은 복사
_items = Array.ConvertAll(items, x => new Item(x.code, x.name)); // 깊은 복사
Array.ForEach(items, x => x.Print());
Array.ForEach(_items, x => x.Print());
}
Array.Reverse(items);
Array.ForEach(items, x => x.Print());
Array.Resize(ref items, 5);
items[4] = new Item(5, "grape");
Array.ForEach(items, 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}");
}
}
List 예시입니다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Test : MonoBehaviour
{
[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 (Item item in items)
item.Print();
{
Item item = new Item(5, "watermelon");
items.Add(item);
bool b = items.Remove(item);
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));
items.Reverse();
{
bool b = items.Exists(x => x.name == "carrot");
print(b);
}
{
bool b = items.TrueForAll(x => x.code > 0);
print(b);
}
{
List<Item> _items = items; // 얕은 복사
_items = items.ConvertAll(x => new Item(x.code, x.name)); // 깊은 복사
_items[0].code = 100;
_items.ForEach(x => x.Print());
}
{
Item item = items.Find(x => x.name == "apple");
item.Print();
}
{
int index = items.FindIndex(x => x.name == "apple");
print(index);
}
{
Item item = items.FindLast(x => x.name == "apple");
item.Print();
}
{
int index = items.FindLastIndex(x => x.name == "apple");
print(index);
}
{
List<Item> _items = items.FindAll(x => x.code > 2);
_items.ForEach(x => x.Print());
}
{
Item[] itemArray = items.ToArray();
List<Item> lst = new List<Item>(itemArray);
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}");
}
}
Dictionary 예시입니다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Test : MonoBehaviour
{
[SerializeField]
Dictionary<string, Item> items = new Dictionary<string, Item>()
{
{ "banana", new Item(2, "banana") },
{ "apple", new Item(1, "apple") }
};
void Start()
{
items.Add("watermelon", new Item(3, "watermelon"));
items.Remove("watermelon");
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 b = items.ContainsValue(_item);
print(b);
}
{
if (items.TryGetValue("apple", out Item _item))
_item.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}");
}
}