Iterating Complex
Coddy C# 여정의 논리 및 흐름 섹션에 포함된 레슨 — 66개 중 14번째.
중첩된 배열과 같은 복잡한 구조를 순회하려면 특별한 접근 방식이 필요합니다.
중첩 루프를 사용하여 2D 배열을 순회합니다:
int[][] matrix = new int[3][];
matrix[0] = new int[] { 1, 2, 3 };
matrix[1] = new int[] { 4, 5, 6 };
matrix[2] = new int[] { 7, 8, 9 };
for (int i = 0; i < matrix.Length; i++)
{
for (int j = 0; j < matrix[i].Length; j++)
{
Console.WriteLine(matrix[i][j]);
}
}문자열 배열을 순회하며 각 문자를 처리하세요:
string[] words = { "hello", "world" };
for (int i = 0; i < words.Length; i++)
{
for (int j = 0; j < words[i].Length; j++)
{
Console.WriteLine(words[i][j]);
}
}가변 배열의 항목 수 세기:
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2, 3 };
jaggedArray[1] = new int[] { 4, 5 };
jaggedArray[2] = new int[] { 6 };
int totalItems = 0;
for (int i = 0; i < jaggedArray.Length; i++)
{
totalItems += jaggedArray[i].Length;
}
Console.WriteLine($"Total items: {totalItems}");챌린지
쉬움countElements라는 메서드를 생성하세요. 이 메서드는:
- 문자열의 가변 배열 (string[][])을 매개변수로 받습니다
- 전체 가변 배열에 있는 요소의 수를 계산합니다
- 총 개수를 반환합니다
예를 들어, 다음과 같이 주어졌을 때:
[
["apple", "orange", "apple"],
["banana", "apple"],
["orange"]
]결과는 6 (3 + 2 + 1)이어야 합니다.
치트 시트
2D 배열을 순회하기 위해 중첩 루프를 사용하세요:
int[][] matrix = new int[3][];
matrix[0] = new int[] { 1, 2, 3 };
matrix[1] = new int[] { 4, 5, 6 };
matrix[2] = new int[] { 7, 8, 9 };
for (int i = 0; i < matrix.Length; i++)
{
for (int j = 0; j < matrix[i].Length; j++)
{
Console.WriteLine(matrix[i][j]);
}
}문자열 배열을 순회하고 개별 문자를 접근하세요:
string[] words = { "hello", "world" };
for (int i = 0; i < words.Length; i++)
{
for (int j = 0; j < words[i].Length; j++)
{
Console.WriteLine(words[i][j]);
}
}가변 배열의 총 항목 수를 세세요:
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2, 3 };
jaggedArray[1] = new int[] { 4, 5 };
jaggedArray[2] = new int[] { 6 };
int totalItems = 0;
for (int i = 0; i < jaggedArray.Length; i++)
{
totalItems += jaggedArray[i].Length;
}
Console.WriteLine($"Total items: {totalItems}");직접 해보기
using System;
public class CountElements
{
// countElements 메서드를 구현하세요
public static int countElements(string[][] nestedArray)
{
// 여기에 코드를 작성하세요
}
}이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
논리 및 흐름의 모든 레슨
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety