// 컬렉션 생성
//Add
//Contains
//단일 요소값 가져오기
//Remove
//Count
//for
//foreach
List
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Study09
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
// 컬렉션 생성
List<Weapon> weapons = new List<Weapon>(5);
//Add
Weapon weapon0 = new Weapon("장검");
weapons.Add(weapon0);
weapons.Add(new Weapon("단검"));
weapons.Add(new Weapon("지팡이"));
weapons.Add(null);
//Contains
bool temp0 = weapons.Contains(weapon0);
//단일 요소값 가져오기
Weapon temp = weapons[0];
Console.WriteLine(temp == weapon0);
Console.WriteLine("{0}, {1}", temp.GetHashCode(), weapon0.GetHashCode());
//Remove
weapons.Remove(weapon0);
//Count
Console.WriteLine(weapons.Count);
//for
for(int i = 0; i < weapons.Count; i++)
{
if (weapons[i] != null)
{
Console.WriteLine(weapons[i].Name);
}
}
//foreach
foreach(Weapon weapon in weapons)
{
Console.WriteLine(weapon.Name);
}
}
}
}
Queue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Study09
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
// 컬렉션 생성
Queue<int> queue = new Queue<int>();
//Add
queue.Enqueue(3);
queue.Enqueue(4);
//Contains
bool temp = queue.Contains(1);
//단일 요소값 가져오기
Console.WriteLine(queue.Peek());
//Remove
queue.Dequeue();
//Count
Console.WriteLine(queue.Count());
//for(x)
//foreach
foreach(int que in queue)
{
Console.WriteLine(que);
}
}
}
}
Stack
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Study09
{
class App
{
//생성자
public App()
{
Console.WriteLine("App");
// 컬렉션 생성
Stack<int> stacks = new Stack<int>();
//Add
stacks.Push(1111);
stacks.Push(2222);
stacks.Push(3333);
//Contains
bool temp = stacks.Contains(2222);
//단일 요소값 가져오기
Console.WriteLine(stacks.Pop());
//Remove(x)
//Count
Console.WriteLine(stacks.Count);
//for(x)
//foreach
foreach(int stack in stacks)
{
Console.WriteLine(stack);
}
}
}
}
'C# > 수업내용' 카테고리의 다른 글
[ C# 9일차 ] char (0) | 2023.01.11 |
---|---|
[ C# 8일차 ] 오버라이딩 (0) | 2023.01.10 |
[ C# 8일차 ] Generic 일반화 Class (0) | 2023.01.10 |
[ C# 8일차 ] Generic 일반화 메서드 (0) | 2023.01.10 |
[ C# 8일차 ] Struct 구조체 (0) | 2023.01.10 |