2017년 5월 24일 수요일

C# 한방에 배우기 개인정리노트 5# 리스트

C# 한방에 배우기 개인정리노트 5#

            리스트
            배열과 비슷 하지만 리스트에 아이템을 추가하면 자동으로 크기가 조정된다.

            List<int> numList = new List<int>(); //리스트 선언
            numList.Add(5);
            numList.Add(15);
            numList.Add(44);

            foreach(int num in numList) //리스트 반복문
            {
                Console.Write(num+", ");
            }
    
            int[] randArray = { 1, 2, 3, 4, 5 };

            numList.AddRange(randArray); //리스트안에 배열요소 삽입 (자료형이 같아야 한다)
            numList.Clear(); //리스트 내용 전부 제거

            List<int> numList2 = new List<int>(randArray);//리스트 선언때 배열 요소 삽입
            List<int> numList3 = new List<int>(new int[] { 1, 2, 3, 4 });

            numList.Insert(1, 10); //특정 인덱스에 아이템 삽입 인덱스를 먼저 입력후 아이템 입력
            numList.Remove(5); //지정한 요소를 가진 첫번째 인덱스를 지운다
            numList.RemoveAt(2); //지정한 인덱스를 지운다.

            for(int i=0; i<numList.Count; i++) //리스트의 길이는 count를 이용하여 구한다
            {
                Console.Write(numList[i] + ", ");
            }            

Console.Write(numList3.IndexOf(4)); //지정된 요소를 포함한 인덱스를 반환 찾지 못하면 -1 반환
Console.Write(numList.Contains(5)); //리스트에 지정된 요소가 있는지 확인 True,False 반환

List<string> strList = new List<string>(new string[] { "Rodun", "Raccoon"});//문자열 리스트 선언

strList.Contains("rodun",StringComparer.OrdinalIgnoreCase); //대소문자를 구분하지 않고 rodun을 찾는다.

strList.Sort(); // 오름차순으로 데이터 정렬

댓글 없음:

댓글 쓰기