수업 012 (04.21 2020) List<> 복습
2020. 4. 21. 10:13
728x90
1. App.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._013
{
class App
{
public App()
{
// List<string> 객체 생성
List<string> itemNameList = new List<string>();
// 추가
itemNameList.Add("장검");
itemNameList.Add("단검");
itemNameList.Add("활");
itemNameList.Add("도끼");
// 검색
string targetItemName = "활";
string foundItemName = "";
#region for statement
/*
for (int i = 0; i < itemNameList.Count; i++)
{
if (itemNameList[i] == targetItemName)
{
// Console.WriteLine("{0}을 찾았습니다.", targetItemName);
foundItemName = itemNameList[i];
break;
}
}
if (foundItemName == "")
{
Console.WriteLine("{0}을 찾을 수 없습니다.", targetItemName);
}
else
{
Console.WriteLine("{0}을 찾았습니다.", foundItemName);
}
*/
#endregion
#region foreach statement
/*
foreach (string itemName in itemNameList)
{
if (itemName == targetItemName)
{
foundItemName == itemName;
break;
}
}
*/
#endregion
if (string.IsNullOrEmpty(foundItemName)) // if(foundItemName == "" || foundItemName == null)
{
Console.WriteLine("{0}을 찾을 수 없습니다. \n", targetItemName);
}
else
{
Console.WriteLine("{0}을 찾았습니다. \n", foundItemName);
}
// 삭제
itemNameList.Remove("활");
// 출력
{
Console.WriteLine(itemNameList[i]);
}
Console.WriteLine("-------");
foreach (string itemName in itemNameList)
{
Console.WriteLine(itemName);
}
}
}
}
|
2. Result

728x90
'C# > Study' 카테고리의 다른 글
수업 012 (04.21 2020) List<> HerbBag (0) | 2020.04.22 |
---|---|
수업 012 (04.21 2020) List<> add, remove, enum (0) | 2020.04.21 |
수업 011 (04.20 2020) List<> (0) | 2020.04.21 |
수업 011 (04.20 2020) 복습 - array (0) | 2020.04.20 |
수업 010 (04.17 2020) Array (0) | 2020.04.17 |