Notice
Recent Posts
Recent Comments
Link
스토리지
[3.17] Stack 연습 1 본문
using System;
namespace Study07
{
public class Item
{
private string name;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
public Item(string name)
{
this.name = name;
}
}
}
using System;
using System.Collections.Generic;
namespace Study07
{
public class App
{
public App()
{
//Stack<Item> 변수 선언
Stack<Item> items;
//Stack<Item> 인스턴스 생성후 변수 할당
items = new Stack<Item>();
//Item 객체 생성
Item sword = new Item("장검");
Item dagger = new Item("대거");
Item spear = new Item("창");
//Stack에 값 추가 Push
items.Push(sword);
items.Push(dagger);
items.Push(spear);
//Stack의 요소 출력 (foreach)
foreach(var i in items)
{
Console.WriteLine(i.Name);
}
Console.WriteLine("--------- pop ----------");
//Stack에서 값 꺼내오기 Pop (출력 : 아이템의 이름)
Console.WriteLine("Pop된 데이터 : {0}", items.Pop().Name);
//Stack의 요소의 수 출력
Console.WriteLine("요소의 수 : {0}", items.Count);
Console.WriteLine("--------- peek ----------");
//Stack의 요소 보기 Peek (출력: 아이템 이름)
Console.WriteLine("스택의 Peek 요소 : {0}", items.Peek().Name);
}
}
}
'Unity > 수업내용(C#)' 카테고리의 다른 글
[3.18] GUID 및 Equatable (0) | 2021.03.18 |
---|---|
[3.17] Queue 연습 1 (0) | 2021.03.17 |
[3.17] 인터페이스 연습 2 (0) | 2021.03.17 |
[3.17] 인터페이스 연습 1 (0) | 2021.03.17 |
[3.17] 배열 및 컬렉션 복습 2 (0) | 2021.03.17 |
Comments