(C#) ArrayList, Hash

장장·2025년 9월 24일

C샵자료구조

목록 보기
5/6

ArrayList

List 자료구조와 비슷하지만 모든 자료형을 다 허용
하지만 아래 문제가 있어 대부분 List로 처리하는 게 좋을 것 같다.

ArrayList의 문제
1. 오브젝트형이라 박싱, 언박싱 성능저하
2. 값 사용시 형변환 필요 (안전하게 사용하려면 주의 필요)

class Program
{
    static void Main(string[] args)
    {
        ArrayList array = new ArrayList();
        List<int> intArray = new List<int>();

        //ArrayList는 모든 자료형이 다들어올 수 있음 (Object형)
        array.Add(1);           
        array.Add("이거되나?");
        array.Add(12.23f);
        array.Add(new Random());
        array.Remove(1);

        //List는 int만 들어올 수 있어서 안전
        intArray.Add(1);
        //intArray.Add("이거되나?"); //에러
        //intArray.Add(12.23f);
        intArray.Remove(1);
    }
}

Hash

해쉬태그 Hash
Dictionary 자료구조와 비슷하지만 오브젝트 형으로 Key와Value값을 저장한다
Object의 단점인 박싱언박싱, 언박싱 시도시 안전하지못한 형변환으로인해 프로그램 비정상 종료 가능성 존재
데이터를 빠르게 찾기 위해 특정 규칙에 따라 숫자(위치)를 반환

Hashtable hashtable = new Hashtable();
hashtable.Add("id", 12);
hashtable["name"] = "홍길동";
hashtable["score"] = 95;

0개의 댓글