์ ๋ค๋ฆญ ์ปฌ๋ ์
(Generic Collection)์ ์กด์ฌ ๋ฐ ์ฌ์ฉ ์ด์
Generic Collection
: object
ํ์์ ๊ธฐ๋ฐํ ๊ธฐ์กด ์ปฌ๋ ์
์ ๋ฌธ์ ๋ค์ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉ๋๊ณ ์๋ค.
Generic(์ผ๋ฐํ)
๊ธฐ๋ฐ์ผ๋ก ๋ง๋ค์ด์ ธ ์๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ์ ์ฌ์ฉํ ํ์์ด ์ ํด์ง๋ฏ๋ก ํ๋ณํ ๋ฐ์์ด ์ค์ด๋ ๋ค.
- ์ปฌ๋ ์
์ด ๊ฐ์ง๊ณ ์๋
์ฑ๋ฅ์์ ๋ฌธ์
๋ฅผ ํด๊ฒฐํ ์ ์๋ค.
โญ๏ธโญ๏ธโญ๏ธ๊ธฐ์กด ์ปฌ๋ ์
์ด ๊ฐ์ง๊ณ ์๋ ์ฑ๋ฅ์์ ๋ฌธ์
- ์ปฌ๋ ์
์ ๋ฐ์ดํฐ๋ฅผ
object
ํ์์ผ๋ก ์ ์ฅํจ. int
, double
, string
๊ณผ ๊ฐ์ ํ์
์ ์ปฌ๋ ์
์ ์ ์ฅํ๊ฒ๋๋ฉด object
ํ์์ผ๋ก ์ ์ฅํ๊ณ ์ปฌ๋ ์
์ด ์ปดํ์ผ๋ ๋ ์ด์ ๊ฐ์ด ํ๋ณํ
์ด ๋งค๋ฒ ์ผ์ด๋๊ฒ๋จ. ๋ํ ์ปดํ์ผ์ ๋ฟ๋ง ์๋๋ผ ์ปฌ๋ ์
๋ด ์์(๋ฐ์ดํฐ)์ ์ ๊ทผํ ๋์๋ ํ๋ณํ
์ด ๊ณ์ํด์ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ ์ปฌ๋ ์
์ ์ฑ๋ฅ์ ๊ด๋ จํ ๋ฌธ์ ๊ฐ ์์
- ์ด๋
Generic Collection
์ ํตํด ํด๊ฒฐํ ์ ์์.
๊ธฐ์กด ์ปฌ๋ ์
๊ณผ ์ผ๋ฐํ(Generic) ๋ฒ์
๊ธฐ์กด |
์ผ๋ฐํ ๋ฒ์ |
ArrayList |
List |
Queue |
Queue |
Stack |
Stack |
Hashtable |
Dictionary<TKey, TValue> |
1. List<T>๋ ArrayList์ ์ผ๋ฐํ(Generic)๋ฒ์
2. Queue<T>๋ Queue์ ์ผ๋ฐํ(Generic)๋ฒ์
3. Stack<T>๋ Stack์ ์ผ๋ฐํ(Generic)๋ฒ์
4. Dictionary<TKey, TValue>๋ Hashtable์ ์ผ๋ฐํ(Generic)๋ฒ์
์ฌ์ฉ ์ฝ๋ ์์
List
using System.Collections;
namespace GenericCollectionEx
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
for (int i = 0; i < 10; i++)
{
list.Add(i);
}
foreach (int i in list)
{
Console.Write($"{i} ");
}
Console.WriteLine();
list.RemoveAt(2);
foreach (int i in list)
{
Console.Write($"{i} ");
}
Console.WriteLine();
list.Insert(2, 2);
foreach (int i in list)
{
Console.Write($"{i} ");
}
Console.WriteLine();
}
}
}
Output
0 1 2 3 4 5 6 7 8 9
0 1 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Queue
using System.Collections;
namespace GenericCollectionEx
{
//Queue<T>
class Program
{
static void Main(string[] args)
{
Queue<int> q = new Queue<int>();
q.Enqueue(100);
q.Enqueue(200);
q.Enqueue(300);
q.Enqueue(400);
q.Enqueue(500);
while(q.Count > 0)
{
Console.WriteLine(q.Dequeue());
}
}
}
}
Output
100
200
300
400
500
Stack
using System.Collections;
namespace GenericCollectionEx
{
//Stack<T>
class Program
{
static void Main(string[] args)
{
Stack<int> stack = new Stack<int>();
stack.Push(11);
stack.Push(22);
stack.Push(33);
stack.Push(44);
stack.Push(55);
while(stack.Count > 0)
{
Console.WriteLine(stack.Pop());
}
}
}
}
Output
55
44
33
22
11
Dictionary<TKey, TValue>
using System.Collections;
namespace GenericCollectionEx
{
//Dictionary<TKey, TValue>
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic["apple"] = "์ฌ๊ณผ";
dic["orange"] = "์ค๋ ์ง";
dic["banana"] = "๋ฐ๋๋";
dic["watermelon"] = "์๋ฐ";
Console.WriteLine(dic["apple"]);
Console.WriteLine(dic["orange"]);
Console.WriteLine(dic["watermelon"]);
Console.WriteLine(dic["banana"]);
}
}
}
Output
์ฌ๊ณผ
์ค๋ ์ง
์๋ฐ
๋ฐ๋๋
์ ์ฒด ์ฝ๋
using System.Collections;
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
for (int i = 0; i < 10; i++)
{
list.Add(i);
}
Console.WriteLine("-----List-----");
foreach (int i in list)
{
Console.Write($"{i} ");
}
Console.WriteLine("\n\n-----Queue-----");
Queue<int> q = new Queue<int>();
q.Enqueue(100);
q.Enqueue(200);
q.Enqueue(300);
q.Enqueue(400);
q.Enqueue(500);
while (q.Count > 0)
{
Console.Write(q.Dequeue()+" ");
}
Console.WriteLine("\n\n-----Stack-----");
Stack<int> stack = new Stack<int>();
stack.Push(11);
stack.Push(22);
stack.Push(33);
stack.Push(44);
stack.Push(55);
while (stack.Count > 0)
{
Console.Write(stack.Pop() + " ");
}
Console.WriteLine("\n\n-----Dictionary-----");
Dictionary<string, string> dic = new Dictionary<string, string>();
dic["apple"] = "์ฌ๊ณผ";
dic["orange"] = "์ค๋ ์ง";
dic["banana"] = "๋ฐ๋๋";
dic["watermelon"] = "์๋ฐ";
Console.Write(dic["apple"] + " ");
Console.Write(dic["orange"] + " ");
Console.Write(dic["watermelon"] + " ");
Console.WriteLine(dic["banana"]);
}
}
Output
-----List-----
0 1 2 3 4 5 6 7 8 9
-----Queue-----
100 200 300 400 500
-----Stack-----
55 44 33 22 11
-----Dictionary-----
์ฌ๊ณผ ์ค๋ ์ง ์๋ฐ ๋ฐ๋๋