🟧 가변 배열
형식
- 데이터형[ ][ ] 배열명;
- 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}
};
namespace ConsoleApp4
{
class Myclass
{
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++)
{
for(int j=0; j < array[i].Length; j++)
{
Console.Write(array[i][j]);
}
Console.WriteLine();
}
}
}
}
namespace ConsoleApp4
{
class Myclass
{
static void Main(string[] args)
{
int[][][] array3 = new int[2][][];
array3[0] = new int[2][];
array3[1] = new int[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]);
}
Console.WriteLine();
}
Console.WriteLine();
}
}
}
}
🟪 배열을 함수로 전달
▶ 일차원 배열을 함수로 전달
int[ ] array = {1,2,3,4};
void func(int[ ] arr) //()안에 int[ ] arr 는 참조배열변수
{
}
func(array);
namespace ConsoleApp4
{
class Myclass
{
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}});
'C#' 카테고리의 다른 글
[C# 기초공부] 파일입출력(1): 파일스트림FileStream/using/ StreamWriter, StreamReader (2) | 2023.01.08 |
---|---|
[C# 기초공부] 배열(3) 배열을 리턴하는 함수 / 배열의 메서드들 (2) | 2023.01.06 |
[C# 기초공부] 배열(1) 일차원배열과 다차원배열 (0) | 2023.01.06 |
[C# 기초공부] 점프문(goto, continue, return, break)/ 예외 처리문(if~else)(try~catch)(try~finally)(throw) (0) | 2023.01.06 |
[C# 기초공부] 선택문 (if~else)(switch, case) (0) | 2023.01.04 |