728x90
반응형

03. 가변 배열


  • 형식

      데이터형[][] 배열명;   // 행과 열을 가지는 가변배열

      int[][] array_name;

 

  • 사용 예(1)
int[][] array = new int[3][];
array[0] = new int[2];   // 각행의 열을 생성
array[1] = new int[3];
array[2] = new int[4];

 

  • 사용 예(2)
int[][] array = new int[3][];
array[0] = new int[2] {1, 2};
array[1] = new int[3] {3, 4, 5};
array[2] = new int[4] {6, 7, 8, 9};

                       또는

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

 

  • 사용 예(3)

     

int[][] array = new int[][];
{
      new int[] {1, 2, 3},
      new int[] {4, 5, 6},
      new int[] {7, 8, 9}
};

 

  • 사용 예(4) // 자주 사용함

     

int[][] array = {
      new int[] {1, 2, 3},
      new int[] {4, 5, 6},
      new int[] {7, 8, 9}
};

 

 

5-3 예제) 가변배열

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _5_3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] array = new int[2][];
            array[0] = new int[3] { 1, 2, 3 };
            array[1] = new int[2] { 4, 5 };

            for (int i = 0; i < array.Length; i++) //array.Length행의 개수
            {
                for (int j = 0; j < array[i].Length; j++) //array[i].Length 열의 개수 
                    Console.Write(array[i][j]); //12345
            }
        }
    }
}

 

5-4 예제) 다차원 가변배열

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _5_4
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][][] array3 = new int[2][][];  // [면][행][열] // 2는 면의 대한 생성
            array3[0] = new int[2][]; // 1면에 2개의 행
            array3[1] = new int[3][]; // 2면에 3개의 행

            array3[0][0] = new int[3] { 1, 2, 3 }; // 열
            array3[0][1] = new int[2] { 4, 5 };    // 열

            array3[1][0] = new int[3] { 6, 7, 8 }; // 열
            array3[1][1] = new int[2] { 9, 10 };   // 열
            array3[1][2] = new int[2] { 11, 12 };  // 열

            for (int i = 0; i < array3.Length; i++) // 면의 개수
            {
                for (int j = 0; j < array3[i].Length; j++) // 행의 개수
                {
                    for (int k = 0; k < array3[i][j].Length; k++) // 열의 개수
                    {
                        Console.Write("{0} ", array3[i][j][k]);
                    }                    
                }
            }
        }
    }
}

 

 

04. 배열을 함수로 전달


1차원 배열을 함수로 전달

int[] array = {1, 2, 3, 4}; // 객체 생성
// arr을 바꾸면 array에도 영향을 끼친다.
void func(int[]arr) // arr: 참조 배열변수
{
}
func(array);

 

5-5 예제)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _5_5
{
    class Program
    {
        static void TransArray(string[] arr)
        {
            string[] HangulDays = { "일", "월", "화", "수", "목", "금", "토" };
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = HangulDays[i];
            }
        }

        static void Main(string[] args)
        {
            string[] Days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
            TransArray(Days);
            foreach (string str in Days)
                Console.Write(str);
        }
    }
}

 

이차원 배열을 함수로 전달

 

  • 이차원 배열을 참조로 전달
int[, ] array2 = {{1, 2, 3}, {4, 5, 6}};
SetArray(array2);

void SetArray(int[, ]arr) // 참조 배열 변수
{
}
// 함수 안에서 아래와 같은 표현으로도 가능
- SetArray(new int[, ] {{1, 2, 3}, {4, 5, 6}});

 

      

728x90
반응형

+ Recent posts