Programming/C# - Window

C#/ 컬렉션(collection)·[ArrayList, Queue, Stack, Hashtable]k

esoog Polaris 2023. 6. 28. 11:17
using System;
using System.Collections;
// 컬렉션 자료구조를 사용하기 위한 시스템 네임스페이스
// ArrayList, Queue, Stack, Hashable 등 이 대표.
namespace UsingList
{
class MainApp
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
// ArrayList는 배열과 달리, 용량 지정 필요 없음!
// 중요 메서드 Add(), Remove(), Insert()
ArrayList list2 = new ArrayList()
{
// 프로퍼티처럼 초기화 가능(ArrayList, Hashtable만)
1,2,3,4
};
for (int i = 0; i < 5; i++)
list.Add(i);
foreach (object obj in list)
Console.Write($"{obj} ");
Console.WriteLine();
list.RemoveAt(2);
foreach (object obj in list)
Console.Write($"{obj} ");
Console.WriteLine();
list.Insert(2, 2);
foreach (object obj in list)
Console.Write($"{obj} ");
Console.WriteLine();
list.Add("abc");
list.Add("def");
for ( int i=0; i<list.Count; i++ )
Console.Write($"{list[i]} ");
Console.WriteLine();
}
}
}
-------------------------------------------------------------------
using System;
using System.Collections;
namespace UsingQueue
{
class MainApp
{
static void Main(string[] args)
{
Queue que = new Queue();
// Queue는 말처럼, 빠르게 입구로 들어와서 출구로 나가는 구조. 통로형
que.Enqueue(1);
// .Enqueue(1) 입구로 입력 메서드
que.Enqueue(2);
que.Enqueue(3);
que.Enqueue(4);
que.Enqueue(5);
while (que.Count > 0)
Console.WriteLine(que.Dequeue());
// .Dequeue() 출구로 출력 메서드
}
}
}
-----------------------------------------------------------------------------
using System;
using System.Collections;
namespace UsingStack
{
class MainApp
{
static void Main(string[] args)
{
Stack stack = new Stack();
// Stack은 출입구가 하나밖에 없음. 맨 위에 있으면 먼저 탈출 구조.
stack.Push(1);
// .Push(1) 밀어넣기 메서드
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(5);
while (stack.Count > 0)
Console.WriteLine(stack.Pop());
// .Pop() 뽑아내기 메서드
}
}
}
--------------------------------------------------------------------------
using System;
using System.Collections;
using static System.Console;
namespace UsingHashtable
{
class MainApp
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
// Hashtable은 키와 값 쌍으로 이뤄진 데이터 자료구조.
// 해싱 작업으로 키를 이용해 컬렉션 주소 바로 계산
{
// 초기화 가능. 프로퍼티처럼 초기화 하면 됨.
{"여섯", 6},
{"일곱", 7},
{"여덟", 8}
}
ht["하나"] = "one";
// x[key] = value; 로 삽입
ht["둘"] = "two";
ht["셋"] = "three";
ht["넷"] = "four";
ht["다섯"] = "five";
WriteLine( ht["하나"] ) ;
WriteLine( ht["둘"] );
WriteLine( ht["셋"] );
WriteLine( ht["넷"] );
WriteLine( ht["다섯"] );
}
}
}
view raw MainApp.cs hosted with ❤ by GitHub
반응형

'Programming > C# - Window' 카테고리의 다른 글

C#/ 예외 처리(try, catch, finally)  (0) 2023.06.28
C#/ 일반화(generic), 인덱서(indexer)k  (0) 2023.06.28
C#/ 배열(array)  (0) 2023.06.28
C#/ 프로퍼티(property)·get/set k  (0) 2023.06.28
C#/ 추상클래스(abstract class)  (0) 2023.06.28