Menu
Coddy logo textTech

배열 메서드

Coddy C# 여정의 기초 섹션에 포함된 레슨. 69개 중 58번째.

배열에는 많은 메서드(기능)가 포함되어 있습니다. 메서드에 접근하려면 다음과 같이 작성합니다:

Array.MethodName(arrayName, otherParameters)

일반적인 Array 메서드:

Clear(array, index, length) - array의 elements를 지우고, elements의 범위를 zero, false 또는 null로 설정합니다:

int[] numbers = {1, 2, 3, 4, 5};
Array.Clear(numbers, 1, 3);
// numbers는 {1, 0, 0, 0, 5}가 됩니다

Reverse(array) - 배열의 요소 순서를 뒤집습니다:

int[] numbers = {1, 2, 3, 4, 5};
Array.Reverse(numbers);
// numbers는 {5, 4, 3, 2, 1}이 됩니다

Sort(array) - array의 elements를 오름차순으로 정렬합니다:

int[] numbers = {5, 2, 9, 1, 5, 6};
Array.Sort(numbers);
// numbers는 {1, 2, 5, 5, 6, 9}가 됩니다

Copy(sourceArray, sourceIndex, destinationArray, destinationIndex, length) - 한 배열에서 다른 배열로 요소 범위를 복사합니다:

int[] source = {1, 2, 3, 4, 5};
int[] destination = new int[5];
Array.Copy(source, 0, destination, 0, 5);
// destination은 {1, 2, 3, 4, 5}가 됩니다

다른 위치를 사용한 예:

int[] source = {1, 2, 3, 4, 5};
int[] destination = new int[7];
Array.Copy(source, 1, destination, 2, 3);
// destination은 {0, 0, 2, 3, 4, 0, 0}이 됩니다
// source의 인덱스 1부터 요소 2, 3, 4를 복사했습니다
// destination의 인덱스 2부터 배치했습니다
challenge icon

챌린지

쉬움

Merge라는 이름의 메서드를 작성하세요. 이 메서드는 두 개의 배열을 인수로 받습니다. 메서드는 두 배열을 정렬된 하나의 배열로 병합하여 반환합니다.

예를 들어 다음 인수인 Merge(new string[] {"1", “4”, “2”}, new string[] {"2", “5”, “9”})["1", “2”, “2”, “4”, “5”, “9”]를 반환합니다.

Array.Copy()를 사용하여 한 배열의 요소를 다른 배열로 복사하세요. 구문은 다음과 같습니다: 

Array.Copy(sourceArray, sourceStartPosition, destinationArray, destinationStartPosition, length)

예:

// Source array
string[] sourceArray = {"1", "2", "3", "4", "5"};

// Destination array
string[] destinationArray = new string[5];

// Copy elements from sourceArray to destinationArray
Array.Copy(sourceArray, 0, destinationArray, 0, 5);

복사 후 Destination 배열: 1 2 3 4 5

직접 해보기

using System;


public class Program {
    public static string[] Merge(string[] arr1, string[] arr2) {
        // 여기에 코드를 작성하세요
    }

    public static void Main(string[] args) {
        string textArr1 = Console.ReadLine();
        string textArr2 = Console.ReadLine();
        string[] arr1 = textArr1.Split(",");
        string[] arr2 = textArr2.Split(",");

        string[] mergedArray = Merge(arr1, arr2);
        Console.WriteLine(string.Join(", ", mergedArray));
    }
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

기초의 모든 레슨

직접 연습해 보세요: 온라인 C# 컴파일러