스토리지

[3.17] 배열 및 컬렉션 복습 1 본문

Unity/수업내용(C#)

[3.17] 배열 및 컬렉션 복습 1

ljw4104 2021. 3. 17. 09:58

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);
            }
        }
    }
}

Comments