Notice
Recent Posts
Recent Comments
Link
스토리지
[3.17] 배열 및 컬렉션 복습 1 본문
1. Array
using System;
using System.Collections.Generic;
namespace Study07
{
public class App
{
public App()
{
//string 배열 변수 선언
string[] arr;
//string 배열 인스턴스 및 변수에 할당
arr = new string[3];
//string 배열의 요소에 값 할당
arr[0] = "Hello";
arr[1] = "World";
arr[2] = "!";
//string 배열의 길이 출력
Console.WriteLine("배열의 길이 : {0}", arr.Length);
//string 배열의 요소 값 출력
Console.WriteLine("배열의 요소 : {0} {1} {2}", arr[0], arr[1], arr[2]);
//for문과 foreach문을 사용해 string 배열의 요소 출력
Console.WriteLine("========== for 문 사용 ==========");
for(int i = 0; i < arr.Length; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.WriteLine("\n========== foreach 문 사용 ==========");
foreach(string i in arr)
{
Console.Write("{0} ", i);
}
Console.WriteLine();
}
}
}
2. List
using System;
using System.Collections.Generic;
namespace Study07
{
public class App
{
public App()
{
//List<string> 변수 선언
List<string> list;
//List<string> 인스턴스 및 변수에 할당
list = new List<string>();
//List<string> 요소에 값 추가
list.Add("Hello");
list.Add("World");
list.Add("!");
//List<string> 의 요소의 수 출력
Console.WriteLine("List의 요소의 수 : {0}", list.Count);
//foreach문을 사용해 List<string>의 요소 출력
foreach(string i in list)
{
Console.Write("{0} ", i);
}
Console.WriteLine();
}
}
}
3. Dictionary
using System;
using System.Collections.Generic;
namespace Study07
{
public class App
{
public App()
{
//Dictionary<int, string> 변수 선언
//ex)
//100, "장검"
//101, "단검"
//102, "활"
Dictionary<int, string> dicWeapon;
//Dictionary<int, string> 인스턴스 및 변수에 할당
dicWeapon = new Dictionary<int, string>();
//Dictionary<int, string> 요소에 값 할당 (키와 값)
dicWeapon.Add(100, "장검");
dicWeapon.Add(101, "단검");
dicWeapon.Add(102, "활");
//Dictionary<int, string>의 요소의 수 출력
Console.WriteLine("Dictionary 요소의 수 : {0}", dicWeapon.Count);
//Dictionary<int, string>의 요소 값 출력 (키로 찾아서, 여기서는 ID)
Console.WriteLine("ID 100의 값 : {0}", dicWeapon[100]);
Console.WriteLine("ID 101의 값 : {0}", dicWeapon[101]);
Console.WriteLine("ID 102의 값 : {0}", dicWeapon[102]);
//foreach문을 사용해 Dictionary<int, string>의 요소 값 출력
foreach(var i in dicWeapon)
{
Console.WriteLine("Key : {0}, Value : {1}", i.Key, i.Value);
}
}
}
}
'Unity > 수업내용(C#)' 카테고리의 다른 글
[3.17] 인터페이스 연습 1 (0) | 2021.03.17 |
---|---|
[3.17] 배열 및 컬렉션 복습 2 (0) | 2021.03.17 |
[3.16] List 컬렉션 연습 1 - 수정(주석추가) (0) | 2021.03.16 |
[3.16] 배열 및 클래스 복습 2 (0) | 2021.03.16 |
[3.16] 배열 및 클래스 복습 1 (0) | 2021.03.16 |
Comments