IT/공부 정리

[UNITY] C# list 사용법

rinix_x 2023. 2. 24. 05:58
반응형

◎ List선언

list<자료형> 변수명 = new List<자료형>() ;

list<자료형> 변수명 = new List<자료형>() {.... } ;

더보기

List<int> num = New List<int>();

List<int> num2 = new List<int>() {1,2,3,4}

◎ 배열 추가 : Add(값)

List<int> num =  new List<int>();

num.Add(0);

 

◎ 중간에 넣기 : Insert(인덱스, 요소)

더보기

class Example : MonoBehavior

{

     void Start(){

          List<String> dogs = new List<string>();

          

          dogs.Add("pomeranian");

          dogs.Add("poodle");

          dogs.Insert(1,"maltese");

 

          foreach ( var dog in dogs){

               Debug.Log(dog);

          }

     }

}

 

output

pormeranian

maltese

poodle

 

◎ 중간에 배열넣기 : InsertRange(인덱스, 배열)

더보기

class Example : MonoBehavior

{

     void Start(){

           List<int> a = new List<int>(){1,2,5,7};

           

           int[] b=new int[3];

           b[0]=3;

           b[1]=4;

           b[2]=6;

 

           a.InsertRange(2,b);

 

           foreach (int i in a){

                Debug.Log(i);

          }

     }

}

 

output

1

2

3

4

6

5

7

 

◎ 맨 뒤에 배열넣기 : AddRange(배열)

더보기

class Example : MonoBehavior

{

     void Start(){

           List<int> a = new List<int>(){1,2,5,7};

           

           int[] b=new int[3];

           b[0]=3;

           b[1]=4;

           b[2]=6;

 

           a.AddRange(b);

 

           foreach (int i in a){

                Debug.Log(i);

          }

     }

}

 

output

1

2

5

7

3

4

6

 

◎ 탐색

- Contain(요소)

list 안에 요소가 있는지 없는지 찾아서 true/false 반환

- Find(item => item > 조건)

조건 보다 큰 첫번 째 요소를 반환

- Exist(element => element > 조건)

list 안에 검색할 조건을 정해주면 true/false반환

- BinarySearch(요소)

list 안에 문자열이 몇번째 배열에 있는지 인덱스를 반환

 

 

◎ 제거

- Remove(요소)

배열안의 특정 요소를 제거

- RemoveAt(요소)

배열안의 특정 인덱스의 요소를 제거

- RemoveAll()

배열안의 정의 한 값과 중복되는 요소들을 모두 제거

- RemoveRange(시작 인덱스, 끝 인덱스)

배열안의 지정된 범위의 요소를 제거

 

◎ 정렬 : Sort()

◎ 개수 세기 : Count()

◎ 비워주기 : Clear()

◎ 정렬 : Sort()

 

 

 

 

 

 

 

 

 

반응형

'IT > 공부 정리' 카테고리의 다른 글

캣멀-롬 스플라인(Catmull-Rom Splines)  (0) 2023.03.19
[UNITY/C#] C#고급 프로그래밍  (0) 2023.03.09
[UNITY] C# 중급 프로그래밍  (0) 2023.02.18
[Unity] 유니티 내부 기초  (0) 2023.02.18
[UNITY] C# 기본 프로그래밍  (0) 2023.02.11