스토리지

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

Unity/수업내용(C#)

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

ljw4104 2021. 3. 17. 10:41

Item class

더보기
using System;

namespace Study07
{
    public class Item
    {
        private int ID;
        private string name;

        public Item()
        {

        }

        public Item(int ID, string name)
        {
            this.ID = ID;
            this.name = name;
        }
        
        public int GetID()
        {
            return ID;
        }

        public string GetItem()
        {
            return this.name;
        }
    }
}

 

1. Array ( using Item )

using System;
using System.Collections.Generic;

namespace Study07
{
    public class App
    {
        public App()
        {
            //Item 배열 변수 선언 
            Item[] items;

            //Item 배열 인스턴스 및 변수에 할당
            items = new Item[3];

            //Item 배열의 요소에 값 할당 
            items[0] = new Item(100, "장검");
            items[1] = new Item(101, "단검");
            items[2] = new Item(102, "활");

            //Item 배열의 길이 출력
            Console.WriteLine("요소의 길이 : {0}", items.Length);

            //Item 배열의 요소 값 출력 (index: 0 ~ n-1)
            Console.WriteLine("요소 출력 : {0} {1} {2}", items[0].GetItem(), items[1].GetItem(), items[2].GetItem());

            //for문과 foreach문을 사용해 Item 배열의 요소 출력 (아이템Id, 아이템이름)
            Console.WriteLine("\nfor문 사용");
            for (int i = 0; i < items.Length; i++)
            {
                Console.WriteLine("ID : {0}, 이름 : {1}",items[i].GetID(),items[i].GetItem());
            }

            Console.WriteLine("\nforeach문 사용");
            foreach (var i in items)
            {
                Console.WriteLine("ID : {0}, 이름 : {1}", i.GetID(), i.GetItem());
            }
        }
    }
}

 

2. List

using System;
using System.Collections.Generic;

namespace Study07
{
    public class App
    {
        public App()
        {
            //List<Item> 변수 선언 
            List<Item> items;

            //List<Item> 인스턴스 및 변수에 할당 
            items = new List<Item>();

            //List<Item> 요소에 값 추가 
            items.Add(new Item(100, "장검"));
            items.Add(new Item(101, "단검"));
            items.Add(new Item(102, "창"));

            //List<Item> 의 요소의 수 출력 
            Console.WriteLine("요소의 수 : {0}", items.Count);

            //foreach문을 사용해 List<Item>의 요소 출력 (아이템 ID, 아이템 이름)
            foreach(var i in items)
            {
                Console.WriteLine("아이템 ID : {0}, 아이템 이름 : {1}", i.GetID(), i.GetItem());
            }
        }
    }
}

 

 

3. Dictionary

using System;
using System.Collections.Generic;

namespace Study07
{
    public class App
    {
        public App()
        {
            //Dictionary<int, Item> 변수 선언 
            Dictionary<int, Item> dicItems;

            //Dictionary<int, Item> 인스턴스 및 변수에 할당 
            dicItems = new Dictionary<int, Item>();

            //Dictionary<int, Item> 요소에 값 할당 (키와 값)
            dicItems.Add(100, new Item(100, "장검"));
            dicItems.Add(101, new Item(101, "단검"));
            dicItems.Add(102, new Item(102, "창"));

            //Dictionary<int, Item>의 요소의 수 출력 
            Console.WriteLine("요소의 수 : {0}", dicItems.Count);

            //Dictionary<int, Item>의 요소 값 출력 (키로 찾아서, 여기서는 ID)
            Console.WriteLine("dicItem[100] = {0}, dicItem[101] = {1}, dicItem[102] = {2}", dicItems[100].GetItem(), dicItems[101].GetItem(), dicItems[102].GetItem());

            //foreach문을 사용해 Dictionary<int, Item>의 요소 값 출력 (아이템 ID, 아이템이름)
            foreach (var i in dicItems)
            {
                Console.WriteLine("Key : {0}, Value : {1}", i.Key, i.Value.GetItem());
            }
        }
    }
}

Comments