코딩 공부/Unity C#
리스트(List)
recordmastd
2024. 7. 4. 23:26
리스트란?
: 크기를 정해야 하는 배열과는 다르게 리스트는 필요에 따라 크기가 늘어날 수 있다\
- List를 사용하려면 네임스페이스가 필요하다. 아래와 같이 지시문을 작성하자.
using System.Collections.Generic;
- 인덱스 번호 입력할 필요없이 Add를 사용한다.
- Resource.Load<GameObject>는 오브젝트를 반환한다
기존 Array를 활용했던 코드를 List를 사용하는 코드로 변경해보자
// create deck and hand
Deck deck = new Deck();
List<Card> hand = new List<Card>();
deck.Shuffle();
// add card, flip over, and print
hand.Add(deck.TakeTopCard());
hand[0].FlipOver();
hand[0].Print();
Console.WriteLine();
// add another card, flip over, and print both cards
hand.Add(deck.TakeTopCard());
hand[1].FlipOver();
hand[1].Print();
hand[0].Print();
- 변수 선언 List로 정정하였음
- 요소 추가 부분을 Add 메서드를 활용하도록 변경함