배열 예제
foreach
foreach(배열요소의 타입 배열요소의변수명 in 배열인스턴스){
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
//생성자
public App()
{
//배열: 동일한 타입의 개체를 그룹화하고 관리할 때 사용
//정수형 배열 arr 정의
int[] arr;//지역변수는 사용하기 전에 반드시 값을 할당해야한다.
arr = null;
Console.WriteLine(arr);
//문자열 형식 배열 변수 itemNames 를 선언하고 초기화 3개
//각 요소에 값을 할당하자
string[] itemNames = new string[3];
itemNames[0] = "장검";
itemNames[1] = "단검";
itemNames[2] = "활";
//배열의 요소를 출력 하자
for(int i=0; i<3; i++)
{
Console.WriteLine(itemNames[i]);
}
//배열 요소를 출력하는 다른 방법
foreach(string itemName in itemNames){
Console.WriteLine(itemName);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
//생성자
public App()
{
Item item0 = new Item("장검");
Item item1 = new Item("단검");
Item item2 = new Item("활");
//아이템을 관리하는 배열
Item[] items = new Item[5];
//아이템을 요소로 할당
items[0] = item0;
items[1] = item1;
items[2] = item2;
//출력
for (int i = 0; i < items.Length; i++)
{
Item item = items[i];
if (item != null)
{
Console.WriteLine("=> {0}", item.Name);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Item
{//멤버변수
public string Name { get; set; }//속성 : 특수메서드 : 대문자로 작성
//생성자
public Item(string name)
{
this.Name = name;
Console.WriteLine("{0}이 생성되었습니다.",this.Name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Inventory
{
//멤버변수
int capacity;
int index;
private Item[] items;
public string Name { get; set; }
//생성자
public Inventory(int capacity) {
this.capacity = capacity;
//아이템을 그룹화, 관리
items = new Item[this.capacity];
}
//메서드
public void AddItem(Item item)
{//아이템 추가
//아이템을 요소로 할당
int lastIndex = this.capacity - 1;
if (this.index > lastIndex) {
Console.WriteLine("공간이 부족 합니다.");
return;
}
//Console.WriteLine("=> {0}", item.name);
//index : 0
//배열의 index 요소에 값넣기
this.items[index] = item;
index++;
}
public Item GetItemByName(string searchName)
{//itenName으로 배열 요소가 있는지 출력
Item foundItem = null;
for (int i = 0; i < this.items.Length; i++)
{
Item item = this.items[i];
if (item != null)
{
if (item.Name == searchName)
{
this.items[i] = null;
foundItem = item;
break;
}
}
}
return foundItem;
}
public void PrintAllItems()
{
for(int i=0; i < this.items.Length; i++)
{
Item item = this.items[i];
if (item != null)
{
Console.WriteLine("{0}",item.Name);
}
else
{
Console.WriteLine("[ ]");
}
}
}
}
}
Inventory.cs
1.
AddItem 에서 this.items[index] = item 에서 아이템을 요소로 할당한다.
index++;에서 다음 인덱스로 넘어가게한다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
//생성자
public App()
{
Inventory inven = new Inventory(5);
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("단검"));
inven.AddItem(new Item("활"));
inven.PrintAllItems();
string searchname = "장검";
Item item = inven.GetItemByName(searchname);
if(item != null)
{
Console.WriteLine("{0}을 꺼냈습니다.", item.Name);
}
else
{
Console.WriteLine("{0}을 찾을 수 없습니다.", searchname);
}
inven.PrintAllItems();
}
}
}
App.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Item
{//멤버변수
public string Name { get; set; }//속성 : 특수메서드 : 대문자로 작성
//생성자
public Item(string name)
{
this.Name = name;
//Console.WriteLine("{0}이 생성되었습니다.",this.Name);
}
}
}
Item.cs
'C# 기초' 카테고리의 다른 글
다차원배열 (0) | 2023.07.25 |
---|---|
배열 연습 (0) | 2023.07.25 |
구조체, 추상 클래스, 인터페이스, 1차원 배열 (0) | 2023.07.24 |
상속, virtual, override, overload, 연결(chaining), casting (0) | 2023.07.24 |
void, 메서드 매개변수, 생성자 연습 (0) | 2023.07.21 |