전기전자/C# 시각화프로그래밍
[C#] 3. 배열기본 : 동적메모리할당 / 가변배열
쑨야미
2021. 4. 13. 16:33
using System;
namespace Day01_03_배열
{
class Program
{
static void Main(string[] args)
{
// *** 동적 배열 *** --> 동적 메모리 할당
int size = int.Parse(Console.ReadLine());
int[] ary = new int[size];
// 10,20,30..... 까지 대입
for(int i=0; i< size; i++)
{
ary[i] = 10 * (i + 1);
}
// *** 가변 배열 *** --> 배열 크기가 수시로 변경
int[] numAry = { };
while(true)
{
Console.Write("숫자-->");
int num = int.Parse(Console.ReadLine());
if (num == 0)
break;
Array.Resize(ref numAry, numAry.Length + 1); //중요!
numAry[numAry.Length - 1] = num;
}
}
}
}