it leader
article thumbnail









List 선언

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

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


배열 추가 Add

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

num.Add(2);





배열 중간에 집어 넣기 Insert

ex ) list.Insert(인덱스, 요소 );

class Program
{
    static void Main()
    {
        List<string> dogs = new List<string>(); // Example list.

        dogs.Add("spaniel"); // Contains: spaniel.
        dogs.Add("beagle"); // Contains: spaniel, beagle.
        dogs.Insert(1, "dalmatian"); // Spaniel, dalmatian, beagle.

        foreach (string dog in dogs) // Display for verification.
        {
            Debug.Log(dog);
        }
    }
}


Output

spaniel
dalmatian
beagle





배열 중간에 배열 추가하기 InsertRange

ex) list.InsertRang( 인덱스, 배열 )


class Program
{
     static void Main()
     {
          List<int> a = new List<int>();
          a.Add(1);
          a.Add(2);
          a.Add(5);
          a.Add(6);
          // Contains: // 1 // 2 // 5 // 6
          int[] b = new int[3];
          b[0] = 7;
          b[1] = 6;
          b[2] = 7;
          a.InsertRange(1, b);
          // Contains: // 1 // 7 [inserted] // 6 [inserted] // 7 [inserted] // 2 // 5 // 6
          foreach (int i in a)
          {
               Debug.Log(i);
          }
     }
}

Output

1 7 6 7 2 5 6





배열 마지막에 배열 추가하기 AddRange


list.AddRange(배열 )


class Program
{
      static void Main()
     {
List<int> a = new List<int>();
a.Add(1);
a.Add(2);
a.Add(5);
a.Add(6);
// Contains: // 1 // 2 // 5 // 6

int[] b = new int[3];
b[0] = 7;
b[1] = 6;
b[2] = 7;
a.AddRange(b);

// Contains: // 1 // 2 // 5 // 6 // 7 [added] // 6 [added] // 7 [added]
foreach (int i in a)
{
   Debug.Log(i);
}
}
}

Output
1 2 5 6 7 6 7




배열 탐색 Contains, Find, Binary


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

list.contains("요소")


list.Add(“cat”);
list.Add(“dog”);
list.Add(“moth”);

if(list.Contains(“dog”);
{
     Debug.Log(“ dog was found”);
}

if( list.Contains(“MOTH”, StringCompare.OrdinalIgnoreCase))
{
     Debug.Log(“ MOTH was found(insentive)”);
     // StringCompare.OrdinalIgnoreCase 메쏘드는 대소문자 구분없이 검색
}

Debug.Log( list.Contains(“fish”);


    OUTPUT     
dog was found
MOTH was found(incentive)
false;






FIND

list.Find( item => item > 조건 );

List<int> list = new List<int>(new int[]{19, 23, 29});

int result = list.Find( item => item > 20);
// 20보다 큰 첫번째 요소를 반환한다.

Debug.Log(“ result : “+ result);



    OUTPUT     

reult : 23




Exist

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

list.Exist(element => element > 10 );






BinarySearch

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

list.BinarySearch( "문자열 ");


Dictionary 과의 호환

class Program
{
    static void Main()
    {
        // Populate example Dictionary.
        var dict = new Dictionary<int, bool>();
        dict.Add(3, true);
        dict.Add(5, false);

        // Get a List of all the Keys.
        List<int> keys = new List<int>(dict.Keys);
        foreach (int key in keys)
        {
             Debug.Log(key);
        }
    }
}


Output

3, 5

배열 요소 제거

Remove
배열안의 특정요소를 제거

dog.Remove("bullDog");

RemoveAt
배열안의 특정 인덱스의 요소를 제거
dog.RemoveAt(1);
//2번째 요소를 제거

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

RemoveRange(시작인덱스, 끝인덱스)
배열 안의 요소들 중 지정된 범위 의 요소들을 제거



LinkedList 사용법

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

linked.AddLast(cat);
linked.AddLast(dog);
linked.AddLast(man);
linked.AddFirst(first);

foreach(item in linked)
{
     Debug.Log(item);
}


Output
first
cat
dog
man


LinkdedList Find 매쏘드


//LinkedList내의 Find매쏘드는 배열 요소를 찾아 주는 역할

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

linked.AddLast(3);
linked.AddLast(5);
linked.AddLast(7);
linked.AddFirst(1);

LinkedListNode<int> node = linked.Find(3);

//node 는 인덱스를 찾아 주는 것이 아니라, 그 요소 자체를 찾아 주는 것임
// 디버그 찍어도 인덱스 번호를 알려주지는 않음

linked.AddBefore( node, 4);

foreach( var value in linked)
{
     Debug.Log( value);
}











profile

it leader

@dev__pixui

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!