하랑이 코딩

컴퓨터공학/자료구조

선형 자료구조 - 리스트(ArrayList) c#

하랑이~! 2024. 5. 31. 10:45

리스트란?


C#의 리스트(List)는 System.Collections.Generic 네임스페이스의 List<T> 클래스를 사용하여 다양한 타입의 데이터를 순차적으로 저장하고 관리할 수 있는 유연한 제네릭 컬렉션입니다.

 

리스트

  • ArrayList - 내부적으로 배열을 사용
  • LinkedList - 링크 포인터를 사용
  • List<T> - 제너릭 타입

 

배열의 특징

  • 생성 시 사용할 공간을 미리 할당한다.
  • 인덱스를 사용 데이터 접근에 빠르다.
  • 데이터의 크기를 변경하지 못한다.

 

리스트특징

  • 데이터의 추가 삭제가 자유롭다.
  • 생성 시 크기를 지정하지 않는다.
  • 리스트를 다른 말로 Dynamic Array라고 부른다.

 

ArrayList 사용법


list.Add사용법

using System;

namespace MyCompiler {
    class Program {
        public static void Main(string[] args) 
        {
            ArrayList list = new ArrayList();

            list.Add(1); // 동적으로 바로 추가
            list.Add(10);
            list.Add(100);
            list.Add(1000);

            for(int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
        }
    }
}

 

list.Insert 사용법

using System;
using System.Collections;

namespace MyCompiler
{
    class Program
    {
        public static void Main(string[] args)
        {
            ArrayList list = new ArrayList();

            list.Add(1); // 동적으로 바로 추가
            list.Add(10);
            list.Add(100);
            list.Add(1000);

            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }

            list.Insert(3, 55); // 3번째 공간에 55추가
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
        }
    }
}

 

list.RemoveAt 사용법

using System;
using System.Collections;

namespace MyCompiler
{
    class Program
    {
        public static void Main(string[] args)
        {
            ArrayList list = new ArrayList();

            list.Add(1); // 동적으로 바로 추가
            list.Add(10);
            list.Add(100);
            list.Add(1000);

            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }

            list.RemoveAt(3); // 3번 공간 삭제
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
        }
    }
}

 

 

ArrayList의 특징

  • 데이터의 크기가 정해져 있지 않고, 동적으로 삽입과 삭제 가능
  • 데이터 타입에 관계없이 삽입이 가능
  • 배열보다 속도가 느리다 
728x90