빈 배열, 배열을 선언하는 다양한 방법을 소개합니다.
1. 빈(Empty) 배열 선언
빈 배열은 보통 아래와 같이 3가지 방법으로 생성할 수 있습니다.
new int[] {};new int[0];{}
아래 예제는 빈 배열을 생성하고 길이를 출력하는 예제입니다.
using System;
namespace Example {
public class Program {
public static void Main(string[] args) {
int[] arr1 = new int[] {};
int[] arr2 = new int[0];
int[] arr3 = {};
Console.WriteLine(arr1.Length);
Console.WriteLine(arr2.Length);
Console.WriteLine(arr3.Length);
}
}
}Output:
0
0
02. 배열 선언과 동시에 특정 객체로 초기화
아래와 같은 방법으로 특정 객체를 초기 값으로 배열을 선언할 수 있습니다.
new int[] {a, b, c};{a, b, c};
아래 예제는 int[] 배열을 정수 3개와 함께 초기화하는 예제입니다.
using System;
namespace Example {
public class Program {
public static void Main(string[] args) {
int[] arr1 = new int[] {1, 2, 3};
int[] arr2 = {1, 2, 3};
Console.WriteLine(string.Join(", ", arr1));
Console.WriteLine(string.Join(", ", arr2));
}
}
}Output:
1, 2, 3
1, 2, 33. 배열 전체를 동일한 값으로 채우기
배열을 초기화할 때, 동일한 값으로 채우는 방법입니다.
Enumerable.Repeat(element, count)는 element를 배열에 count 개수만큼 추가하고 Enumerable로 리턴합니다.
배열로 변환하려면 ToArray()를 호출하면 됩니다.
아래는 크기가 10인 배열을 생성하고 0으로 가득 채우는 예제입니다.
using System;
namespace Example {
public class Program {
public static void Main(string[] args) {
int[] array = Enumerable.Repeat(0, 10).ToArray();
Console.WriteLine(string.Join(", ", array));
}
}
}Output:
0, 0, 0, 0, 0, 0, 0, 0, 0, 03.1 Array.Fill()을 이용한 방법
배열을 생성한 뒤에, Array.Fill(array, element) 함수로 배열을 element로 채울 수 있습니다.
아래 예제는 크기가 10인 배열을 0으로 채우는 예제입니다.
using System;
namespace Example {
public class Program {
public static void Main(string[] args) {
int[] array = new int[10];
Array.Fill(array, 0);
Console.WriteLine(string.Join(", ", array));
}
}
}Output:
0, 0, 0, 0, 0, 0, 0, 0, 0, 04. 연속된 숫자로 배열 초기화
Enumerable.Range(start, count)는 start부터 연속적인 숫자를 count 개수만큼 배열에 추가합니다.
아래 예제는 0부터 9까지의 숫자가 추가된 배열을 생성합니다.
using System;
namespace Example {
public class Program {
public static void Main(string[] args) {
int[] array = Enumerable.Range(0, 10).ToArray();
Console.WriteLine(string.Join(", ", array));
}
}
}Output:
0, 1, 2, 3, 4, 5, 6, 7, 8, 94.1 for문을 이용한 방법
배열 생성 후, for문으로 배열을 연속된 숫자로 초기화할 수 있습니다.
using System;
namespace Example {
public class Program {
public static void Main(string[] args) {
int[] array = new int[10];
int n = 0;
for (int i = 0; i < array.Length; i++) {
array[i] = n;
n++;
}
Console.WriteLine(string.Join(", ", array));
}
}
}Output:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9Loading script...
Related Posts
- C# 바이트(Byte) 배열을 문자열로 변환
- C# String.format() 함수 알아보기
- C# - foreach (Array, List, Dictionary)
- C# 프로퍼티(Property) Get/Set 함수
- C# - Dictionary에서 key, value 가져오기
- C# - Dictionary 순회, foreach, for 루프
- C# - Dictionary에서 key, value 삭제
- C# - Dictionary.add()로 데이터 추가
- C# - Dictionary 선언 및 초기화
- C# - 문자열을 Double, Float으로 변환
- C# - 문자열을 리스트로 변환
- C# - 두 날짜/시간 비교, DateTime.Compare()
- C# - 날짜 문자열을 DateTime으로 변환
- C# - Sleep, 몇 초간 지연시키기
- C# - 날짜 계산, DateTime 시간 더하기 빼기
- C# - Timestamp(millisecond)를 DateTime 객체로 변환
- C# - 현재 시간 가져오기, DateTime
- C# - 문자열 리스트를 문자열로 변환
- C# - 리스트 복사 (얕은 복사, 깊은 복사)
- C# - 2차원 리스트 선언 및 초기화
- C# - 리스트 선언 및 초기화
- C# - 리스트 길이, 크기 가져오기
- C# - 리스트 합계, 평균 계산
- C# - 리스트 요소 제거 (RemoveAt, RemoveAt, RemoveAll)
- C# - 리스트에서 빈 문자열, null 제거
- C# - 리스트 두개로 분리, n개로 나누기
- C# - 문자열 뒤집기, Reverse()
- C# - 2차원 배열 복사, Clone
- C# - 문자열 배열을 문자열로 변환
- C# - 2차원 배열 선언, 초기화 방법
- C# - 배열 길이, 2차원 배열 길이
- C# - 두개의 배열을 하나로 합치기
- C# - 배열 중복 요소 제거
- C# - 배열에서 특정 요소 제거
- C# - Int 배열 정렬 (오름차순, 내림차순)