์ปฌ๋ ์ ์ด๋ ๊ฐ์ ์ฑ๊ฒฉ์ ๋ ๋ฐ์ดํฐ์ ๋ชจ์์ ๋ด๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋งํ๋ค.
public abstrack class Array : ICloneable, IList, ICollection, IEnumerable
- ArrayList
๊ฐ์ฅ ๋ฐฐ์ด๊ณผ ๋ฎ์ ์ปฌ๋ ์ ์ด๋ค.
์ปฌ๋ ์ ์ ์์์ ์ ๊ทผํ ๋๋ [ ] ์ฐ์ฐ์๋ฅผ ์ด์ฉํ๊ณ , ํน์ ์์น์ ์๋ ์์์ ๋ฐ์ดํฐ๋ฅผ ์์๋ก ํ ๋นํ ์ ์๋ค.
ํํธ, ๋ฐฐ์ด๊ณผ๋ ๋ฌ๋ฆฌ ์ปฌ๋ ์ ์ ์์ฑํ ๋ ์ฉ๋์ ๋ฏธ๋ฆฌ ์ง์ ํ ํ์ ์์ด ํ์์ ๋ฐ๋ผ ์๋์ผ๋ก ๊ทธ ์ฉ๋์ด ๋์ด๋๊ฑฐ๋ ์ค์ด๋ ๋ค.
ArrayList์์ ๊ฐ์ฅ ์ค์ํ ๋ฉ์๋๋ Add(), RemoveAt(), Insert() ์ด๋ ๊ฒ ์ธ ๊ฐ์ด๋ค.
Add() : ์ปฌ๋ ์ ์ ๊ฐ์ฅ ๋ง์ง๋ง์ ์๋ ์์ ๋ค์ ์ ์์๋ฅผ ์ถ๊ฐ
RemoveAt() : ํน์ ์ธ๋ฑ์ค์ ์๋ ์์๋ฅผ ์ ๊ฑฐ
Insert() : ์ํ๋ ์์น์ ์ ์์๋ฅผ ์ฝ์
<์์ >
using System;
using System.Collections;
namespace MoreOnArray
{
class MainApp
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
for (int i = 10; i < 60; i+=10)
list.Add(i);
foreach (var obj in list)
Console.Write($"{obj} "); //10 20 30 40 50
Console.WriteLine();
list.RemoveAt(2);
foreach (var obj in list)
Console.Write($"{obj} "); //10 20 40 50
Console.WriteLine();
list.Insert(2,"๋ฐ๋ณด");
foreach (var obj in list)
Console.Write($"{obj} "); //10 20 ๋ฐ๋ณด 40 50
Console.WriteLine();
list.Add("abc");
list.Add("def");
foreach (var obj in list)
Console.Write($"{obj} "); //10 20 ๋ฐ๋ณด 40 50 abc def
Console.WriteLine();
}
}
}
ArrayList๊ฐ ๋ค์ํ ํ์์ ๊ฐ์ฒด๋ฅผ ๋ด์ ์ ์๋ ์ด์ ๋ ๋ค์์ Add.Inset() ๋ฉ์๋ ์ ์ธ์ ๋ณด๋ฉด ์ ์ ์๋ค.
public virtual int Add(Object value)
public virtual void Inset(int index, Object value)
object ํ์์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ๊ณ ์๋ค.
๋ชจ๋ ํ์์ object๋ฅผ ์์ํ๋ฏ๋ก object ํ์์ผ๋ก ๊ฐ์ค๋ ์ ์๋ค.
Add() ๋ฉ์๋์ int ํ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๋๋ผ๋ ์ ์ ํ์ ๊ทธ๋๋ก ์ ๋ ฅ๋๋ ๊ฒ์ด ์๋๋ผ object ํ์์ผ๋ก ๋ฐ์ฑ๋์ด ์ ๋ ฅ๋๋ ๊ฒ์ด๋ค.
๋ฐ๋๋ก ArrayList์ ์์์ ์ ๊ทผํด์ ์ฌ์ฉํ ๋๋ ์๋์ ๋ฐ์ดํฐ ํ์์ผ๋ก ์ธ๋ฐ์ฑ์ด ์ด๋ฃจ์ด์ง๋ค.
๋ฐ์ฑ๊ณผ ์ธ๋ฐ์ฑ์ ์์ง ์์ ์ค๋ฒํค๋๋ฅผ ์๊ตฌํ๋ ์์ ์ด๋ค.
ArrayList๊ฐ ๋ค๋ฃจ๋ ๋ฐ์ดํฐ๊ฐ ๋ง์ผ๋ฉด ๋ง์์ง์๋ก ์ฑ๋ฅ ์ ํ๋ ๋์ฑ ๋์ด๋๋ค.
์ด๊ฒ์ ArrayList๋ง์ ๋ฌธ์ ๊ฐ ์๋๋ผ Stack, Queue, Hashtable ๋ฑ์ ์ปฌ๋ ์ ๋ ๊ฐ์ง๊ณ ์๋ค.
์ผ๋ฐํ ์ปฌ๋ ์ ์ ์ฐ๋ฉด ๋ฌธ์ ๊ฐ ํด์๋๋ค.
- Queue
๋ฐ์ดํฐ๋ ์์ ์ ์ฐจ๋ก๋๋ก ์ ๋ ฅํด๋๋ค๊ฐ ์ ๋ ฅ๋ ์์๋๋ก ํ๋์ฉ ๊บผ๋ด ์ฒ๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ค.
์ ๋ ฅ์ ์ค์ง ๋ค์์, ์ถ๋ ฅ์ ์์์๋ง ์ด๋ฃจ์ด์ง๋ค.
Queue๋ ์์ฒญ๋๊ฒ ๋ค์ํ ๋ถ์ผ์ ์ฌ์ฉ๋๊ณ ์๋ค.
OS์์ CPU๊ฐ ์ฒ๋ฆฌํด์ผ ํ ์์ ์ ์ ๋ฆฌํ ๋, ํ๋ฆฐํฐ๊ฐ ์ฌ๋ฌ ๋ฌธ์๋ฅผ ์ถ๋ ฅํ ๋, ์ธํฐ๋ท ๋์์ ์คํธ๋ฆฌ๋ฐ ์๋น์ค์์ ์ฝํ ์ธ ๋ฅผ ๋ฒํผ๋งํ ๋ ๋ฑ...
์ ๋ ฅ : Enqueue()
์ถ๋ ฅ : Dequeue()
<์์ >
using System;
using System.Collections;
namespace Collections
{
class MainApp
{
static void Main(string[] args)
{
Queue que = new Queue();
que.Enqueue(1);
que.Enqueue(2);
que.Enqueue(3);
que.Enqueue(4);
que.Enqueue(5);
que.Enqueue(6);
que.Enqueue(7);
que.Enqueue(8);
que.Enqueue(9);
que.Enqueue(10);
while (que.Count > 0)
Console.WriteLine(que.Dequeue());
}
}
}
<์ถ๋ ฅ>
1
2
3
4
5
6
7
8
9
10
- Stack
์์
using System;
using System.Collections;
namespace Collections
{
class MainApp
{
static void Main(string[] args)
{
Stack que = new Stack();
que.Push(1);
que.Push(2);
que.Push(3);
que.Push(4);
que.Push(5);
que.Push(6);
que.Push(7);
que.Push(8);
que.Push(9);
que.Push(10);
while (que.Count > 0)
Console.WriteLine(que.Pop());
}
}
}
์ถ๋ ฅ
10
9
8
7
6
5
4
3
2
1
- Hashtable
์ฌ์ book์ ํค๋ก, ์ฑ ์ ๊ฐ์ผ๋ก ์ ๋ ฅํ๋ ์
ํ์ ์๋๊ฐ ๋น ๋ฅด๊ณ ์ฌ์ฉํ๊ธฐ๋ ํธํจ!
using System;
using System.Collections;
namespace Collections
{
class MainApp
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht["book"] = "์ฑ
";
ht["Worker"] = "์๋ฆฌ์ฌ";
ht["tweet"] = "๋จธ์คํฌ์๋์ง";
Console.WriteLine(ht["book"]); //์ฑ
Console.WriteLine(ht["Worker"]); //์๋ฆฌ์ฌ
Console.WriteLine(ht["tweet"]); //๋จธ์คํฌ์๋์ง
}
}
}
๋ฐฐ์ด๊ณผ ๋ค๋ฅธ ์ ์ ๋ฐฐ์ด์ด ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ์์์ ์์น๋ก ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ๋ ๋ฐ๋ฉด์,
Hashtable ์ปฌ๋ ์ ์ ํค ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๋ค๋ ๊ฒ์ด๋ค.
์์ ์์ ์์๋ ๋ฌธ์์ด์ ์ฌ์ฉํ์ง๋ง ์ด๋ค ํ์์ด๋ ํค๋ก ์ฌ์ฉํ ์ ์๋ค.
int๋ float ํ์๋ ํด๋์ค๋!
Hashtable์ ๋ฐฐ์ด์์ ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํด์ ๋ฐฐ์ด ์์์ ์ ๊ทผํ๋ ๊ฒ์ ์คํ๋ ํ์ ์๋๋ฅผ ์๋ํ๋ค.
(ํ์ ์๋๊ฐ ๊ฑฐ์ ์์๋์ง ์๋๋ค.)
ArrayLlist์์ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ฐพ์ผ๋ ค๋ฉด ์ปฌ๋ ์ ์ ์ ๋ ฌํด ์ด์ง ํ์์ ์ํํ๊ฑฐ๋ ์์ฐจ์ ์ผ๋ก ๋ฆฌ์คํธ๋ฅผ ํ์ํด๋๊ฐ์ง๋ง, Hashtable์ ํค๋ฅผ ์ด์ฉํด์ ๋จ๋ฒ์ ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋ ์ปฌ๋ ์ ๋ด์ ์ฃผ์๋ฅผ ๊ณ์ฐํด๋ธ๋ค.
์ด ์์ ์ ํด์ฑ์ด๋ผ๊ณ ํ๋๋ฐ Hashtable์ ์ด๋ฆ์ ์ด ์๊ณ ๋ฆฌ์ฆ์์ ์ ๋ํ ๊ฒ์ด๋ค.
<์ปฌ๋ ์ ์ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ>
ArrayList, Queue, Stack์ ๋ฐฐ์ด์ ๋์์ ๋ฐ์ ๊ฐ๋จํ๊ฒ ์ด๊ธฐํ๋ฅผ ์ํํ ์ ์๋ค.
์ปฌ๋ ์ ์ ์์ฑ์๋ฅผ ํธ์ถํ ๋ ๋ฐฐ์ด ๊ฐ์ฒด๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋๊ธฐ๋ฉด
์ปฌ๋ ์ ๊ฐ์ฒด๋ ํด๋น ๋ฐฐ์ด์ ๋ฐํ์ผ๋ก ๋ด๋ถ ๋ฐ์ดํฐ๋ฅผ ์ฑ์ด๋ค.
๋ค์์ ์ปฌ๋ ์ ์ ์ด๊ธฐํ๋ฅผ ์ํํ๋ ์์ ์ฝ๋์ด๋ค.
<์์ >
using System;
using System.Collections;
namespace Collections
{
class MainApp
{
static void Main(string[] args)
{
int[] arr = new int[] { 1, 2, 3, 4 };
ArrayList list = new ArrayList(arr);
Stack stack = new Stack(arr);
Queue queue = new Queue(arr);
ArrayList list2 = new ArrayList() { 1, 2, 3, 4 };
foreach(var a in list)
{
Console.WriteLine($"{a} ");
}
Console.WriteLine();
foreach (var a in stack)
{
Console.WriteLine($"{a} ");
}
Console.WriteLine();
foreach (var a in queue)
{
Console.WriteLine($"{a} ");
}
Console.WriteLine();
foreach (var a in list2)
{
Console.WriteLine($"{a} ");
}
Console.WriteLine();
}
}
}
<์ถ๋ ฅ>
1
2
3
4
4
3
2
1
1
2
3
4
1
2
3
4
Hashtable์ ์ด๊ธฐํํ ๋๋ ๋์ ๋๋ฆฌ ์ด๊ธฐ์๋ฅผ ์ด์ฉํ๋ค.
Hashtable ht = new Hashtable()
{
["ํ๋"] = 1,
["๋"] = 2,
["์
"] = 3
};
์๋์ ๊ฐ์ด Hashtable์ ์ด๊ธฐํํ ๋๋ ์ปฌ๋ ์
์ด๊ธฐ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
Hashtable ht2 = new Hashtable()
{
{ "ํ๋",1},
{"๋",2 },
{"์
",3}
};
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
55. foreach๊ฐ ๊ฐ๋ฅํ ๊ฐ์ฒด ๋ง๋ค๊ธฐ (0) | 2022.02.27 |
---|---|
54. ์ธ๋ฑ์ (0) | 2022.02.25 |
52. ๊ฐ๋ณ๋ฐฐ์ด (0) | 2022.02.25 |
51. ๋ค์ฐจ์๋ฐฐ์ด (0) | 2022.02.25 |
50. 2์ฐจ์ ๋ฐฐ์ด (0) | 2022.02.24 |