ArrayList
๋ฐฐ์ด๊ณผ ๊ฐ์ฅ ์ ์ฌํ ํํ์ ์๋ฃ๊ตฌ์กฐ๋ก, ๋ฐฐ์ด๊ณผ ๋ค๋ฅธ ์ ์ด๋ผ๋ฉด ์ด๊ธฐ ์ฉ๋์ด ์ ํด์ ธ์์ง ์๋ค๋ ์ ์ด๋ค. ์ฆ, ์ถ๊ฐํ๋ ๋งํผ๋์ด๋๋ค.
namespace CollectionSample
{
class MainApp
{
static void Main(string[] args)
{
ArrayList arrayList = new ArrayList();
arrayList.Add("Apple");
arrayList.Add("Banana");
arrayList.Add("Orange");
foreach (object item in arrayList)
{
Console.WriteLine(item); // Apple Banana Orange
}
}
}
}
๋ฐฐ์ด๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ธ๋ฑ์ค๋ก ์ถ๋ ฅํ ์๋ ์๋ค.
Console.WriteLine(arrayList[1]); // 1๋ฒ์งธ ์ธ๋ฑ์ค ์์ ์ถ๋ ฅ => Banana
์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํด์ ์ํ๋ ์์น์ ์์๋ฅผ ์ญ์ ํ ์๋ ์๊ณ , ์ํ๋ ์์น์ ์ฝ์ ํ ์๋ ์๋ค.
arrayList.RemoveAt(1); // Banana ์ญ์ => Apple Orange
arrayList.Insert(0, "Peach"); // Peach ์ฝ์
=> Peach Apple Orange
Queue
Queue๋ ๋๊ธฐ์ค์ฒ๋ผ ๋จผ์ ๊ธฐ๋ค๋ฆฐ ์ฌ๋์ด ๋จผ์ ๋จผ์ ๋๊ฐ๋ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
Queue queue = new Queue();
// ๋ฐ์ดํฐ ์ถ๊ฐ
queue.Enqueue("Apple");
queue.Enqueue("Banana");
queue.Enqueue("Orange");
Queue๋ ๋จผ์ ๋๊ฐ๋๋๋ก ์์ผ๋ก ์์๊ฐ ๋น๊ฒจ์ง๋ฉฐ, Dequque()๋ฅผ ์ฌ์ฉํ๋ค.
// ๋ฐ์ดํฐ ์ ๊ฑฐ ๋ฐ ์ถ๋ ฅ
while (queue.Count > 0) {
string item = (string)queue.Dequeue(); // Apple, Banana, Orange ์์ผ๋ก ๋ด๊น
Console.WriteLine("Removed: " + item);
}
Stack
Stack์ Queue์ ๋ฐ๋๋ก ์์ด๋ ์๋ฃ๊ตฌ์กฐ๋ก, ๋์ค์ ์์ธ ๊ฒ์ด ๋จผ์ ๋๊ฐ๋ค.
Stack stack = new Stack();
// ๋ฐ์ดํฐ ์ถ๊ฐ
stack.Push("Apple");
stack.Push("Banana");
stack.Push("Orange");
Stack์ ๋จผ์ ๋ค์ด์จ ์์ผ๋ก ์ ๊ฑฐ๊ฐ ๋๋ฉฐ, Pop()์ ์ฌ์ฉํ๋ค.
// ๋ฐ์ดํฐ ์ ๊ฑฐ ๋ฐ ์ถ๋ ฅ
while (stack.Count > 0) {
string item = (string)stack.Pop();
Console.WriteLine("Removed: " + item);
}
HashTable
HashTable์ key-value๊ฐ์ ๊ฐ๋ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
Hashtable hashtable = new Hashtable();
// ๋ฐ์ดํฐ ์ถ๊ฐ
hashtable.Add("A", "Apple");
hashtable.Add("B", "Banana");
hashtable.Add("O", "Orange");
HashTable์ ํด๋น key๋ก value๊ฐ์ ๋ถ๋ฌ์ฌ์ ์๋ค. ๋ง์น ์ธ๋ฑ์ค์ฒ๋ผ ๋ง์ด๋ค.
// ๋ฐ์ดํฐ ์ ๊ทผ ๋ฐ ์ถ๋ ฅ
Console.WriteLine("Value for key 'A': " + hashtable["A"]);
Console.WriteLine("Value for key 'B': " + hashtable["B"]);
Console.WriteLine("Value for key 'O': " + hashtable["O"]);
'๐ฅ๏ธ > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ด๊ฒ์ด C#์ด๋ค] Chapter10: ์ฐ์ต๋ฌธ์ (0) | 2023.08.21 |
---|---|
[C#] ์ธ๋ฑ์ (0) | 2023.08.21 |
[C#] ์ด์ฐจ์ ๋ฐฐ์ด๊ณผ ๊ฐ๋ณ ๋ฐฐ์ด (0) | 2023.08.21 |
[C#] ๋ฐฐ์ด ๋ค๋ฃจ๊ธฐ (0) | 2023.08.21 |
[์ด๊ฒ์ด C#์ด๋ค] Chapter09: ์ฐ์ต๋ฌธ์ (0) | 2023.08.20 |