공부/C#

컬렉션(Collection), 제네릭 컬렉션(Generic Collection) - 예) ArrayList, List<T>

미다손 2016. 5. 11. 02:33

//컬렉션(Collection), 제네릭 컬렉션(Generic Collection)


//가장 큰 차이점

//컬렉션은 아무거나 잡다하게 다 집어 넣어 사용 할 수 있고

//제네릭 컬렉션은 제네릭 타입<T>을 집어넣어 해당 타입만 컨트롤 하는 것이다.

//안정성이나 속도 면에서 제네릭 컬렉션이 좋다.


            //컬렉션 - 템플릿 없음

            //상단에 using System.Collections; 추가

            ArrayList array = new ArrayList(); //std::vector 비슷한 녀석

            array.Add(10);

            array.Add(20);

            array.Add(15.5f);

            array.Add("가나다");

            array.Add(c1);

            array.Add(s1);  //struct는 박싱이 일어난다.

            //자료형이나 class, struct 구분없이 전부 다 들어갈 수 있다.

            for (int i = 0; i < array.Count; ++i )

            {

                Console.WriteLine(array[i]);

            }

            Console.WriteLine();

            //그외

            //HashTable //std::map 과 비슷

            //Queue

            //Stack



//===================================================//

            //제네릭 컬렉션 - <T> 템플릿 형식

            //std::vector<T> 같은 녀석

            List<int> numList = new List<int>();  //타입이 지정되어야 한다.

            numList.Add(19);

            numList.Add(30);

            numList.Add(54);

            //numList.Add(1.15);  //타입이 안맞아 에러

            foreach(int node in numList)

            {

                Console.Write(node + " ");

            }

            Console.WriteLine();


            //std::list<> 같은 녀석

            LinkedList<int> linked = new LinkedList<int>();

            linked.AddLast(50);

            linked.AddLast(60);

            linked.AddLast(70);

            linked.AddFirst(40);

            foreach (int node in linked)

            {

                Console.Write(node + " ");  // 40 50 60 70

            }

            Console.WriteLine();

            

            //그외

            //Dictionary<key, value> //std::map

            //Queue<T>

            //Stack<T>