Unity 기본5 - C# 배열에 대해

1. 배열

 

데이터를 저장하는 공간인 변수

 

그런데 데이터 하나당 변수를 하나씩 만들면 비효율적

 

데이터가 100개면 100개의 변수를 모두 만들어 관리하기는 어렵다.

 

하나의 이름으로 여러개의 데이터를 모아 저장하는 자료구조

 

 

1) 선언하는 방법?

 

int[] a;

 

a는 정수형의 배열

 

혹은 string[] a; 하면 문자열의 배열

 

(자료형)[] a; 해서 해당 자료형의 배열을 선언

 

선언만 되었지, 아무런 값도 지정되어 있지 않아 출력하면 에러남

 

 

 

 

2) 초기화가 필요함

 

a라는 이름을 가진 int배열에 5개의 정수가 들어갈 배열이라고 초기화

 

int[] a = new int[5];

 

정수가 들어갈 수 있는 방 5개짜리 배열을 만들고 이름을 a라고 한다

 

이렇게 초기화된 상태면, 출력할때 int배열인 것을 인식함

 

 

 

 

 

zero based index로 a[0], a[1], a[2], a[3], a[4] 공간을 가진다

 

그런데 현재 어떤 값을 안에 넣을지는 지정하고 있지는 않다

 

하지만 C#은 기본값으로 들어가 있는 값들이 정해져있는데

 

숫자형 배열은 0, bool형 배열은 false, 그 이외에는 null이라고 함

 

실제로 a[0], a[1], a[2], a[3], a[4] 출력하면 0이 나오네

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

        int[] a = new int[5];

        for (int i = 0; i < 5; i++)
        {
            Debug.Log(a[i]);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

 

 

 

 

3) 선언과 동시에 초기화

 

int[] a = new int[5] {2,4,5,7,9}; 하면 a = [2,4,5,7,9]로 초기화되어 있음

 

여기서 new int[5]는 생략해서 int[] a = {2,4,5,7,9}; 해도 동일함

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

        int[] a = new int[5] { 2, 4, 5, 7, 9 };

        for (int i = 0; i < 5; i++)
        {
            Debug.Log(a[i]);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

 

 

 

 

수가 100개이면 int[] a = {a0,a1,a2,a3,...a99};까지 100개를 다 쓸수는 없으니까

 

int[] a = new int[100]; 이렇게 해서 반복문을 이용해 a[i]에 접근해서 수를 집어넣는게 보통

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

        int[] a = new int[5];

        for (int i = 0; i < 5; i++)
        {
            a[i] = i * 3;
        }

        for (int i = 0; i < 5; i++)
        {
            Debug.Log(a[i]);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

 

 

 

 

2. 다차원 배열

 

int[,] a = new int[4,6];

 

행이 4개이고 열이 6개인 4*6의 2차원 배열

 

3차원 배열은 

 

int[,,] a = new int[4,6,7]; 이렇게 할 수 있을 것임

 

배열은 32차원까지 정의할 수 있고 40억개 정도의 원소를 가질 수 있고 최대 용량은 2기가라고 함

 

보통 3차원 넘는 배열은 잘 쓰지 않는다 코드 읽기가 불편함

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

        int[,] a = new int[4, 6];

        for (int y = 0; y < 4; y++)
        {
            for (int x = 0; x < 6; x++)
            {
                a[y, x] = y * 6 + x;
            }
        }

        for (int y = 0; y < 4; y++)
        {
            for (int x = 0; x < 6; x++)
            {
                Debug.Log("y " + y + ", x " + x + "=" + a[y, x]);
            }
        }

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

 

 

 

 

3. 가변 배열

 

배열의 원소로 배열을 사용하는 배열

 

예를 들어 크기가 5인 배열에서 각각 a[0], a[1], a[2], a[3], a[4]가 크기 3, 2, 4, 2, 3인 배열을 또 가지고 있음

 

배열의 원소인 배열의 크기가 서로 다를 수 있음

 

그러다보니 선언할 때 

 

int[][] a = new int[5][];로 마지막 대괄호 부분은 비워둔다

 

 

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloWorld : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int[][] a = new int[3][];

        a[0] = new int[3] { 0, 1, 2 };
        a[1] = new int[5] { 4, 6, 7, 5, 1 };
        a[2] = new int[4] { 3, 7, 9, 10 };

        Debug.Log(a[2]);
        Debug.Log(a[0][1]);
        Debug.Log(a[1][3]);
        Debug.Log(a[2][2]);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

 

 

파이썬처럼 생각하면 

 

[[0,1,2], [4,6,7,5,1], [3,7,9,10]]으로 만든 것인데

 

a[0][1]은 [0,1,2]에서 1번 원소 1

 

a[1][3]은 [4,6,7,5,1]에서 3번 원소 5

 

a[2][2]은 [3,7,9,10]에서 2번 원소 9

 

 

TAGS.

Comments