Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Jagged arrays in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Jagged arrays
Declare jagged array variable
↓
Allocate outer array (rows)
↓
For each row: allocate inner array (columns)
↓
Access elements using two indices: arr[row
↓
Use nested loops to traverse or modify elements
Jagged arrays are arrays of arrays where each inner array can have different lengths. You first create the outer array, then create each inner array separately.
Execution Sample
C Sharp (C#)
int[][] jagged = new int[2][];
jagged[0] = new int[3] {1, 2, 3};
jagged[1] = new int[2] {4, 5};
int val = jagged[0][1];
This code creates a jagged array with 2 rows, assigns arrays of different lengths to each row, and accesses an element.
Execution Table
StepActionOuter Array StateInner Array AllocatedAccess/Value
1Declare jagged array variablenullnonenone
2Allocate outer array with 2 rows[null, null]nonenone
3Allocate inner array at jagged[0] with length 3[array, null]jagged[0] = [0,0,0]none
4Assign values to jagged[0][[1,2,3], null]jagged[0] = [1,2,3]none
5Allocate inner array at jagged[1] with length 2[[1,2,3], array]jagged[1] = [0,0]none
6Assign values to jagged[1][[1,2,3], [4,5]]jagged[1] = [4,5]none
7Access jagged[0][1][[1,2,3], [4,5]]none2
8Access jagged[1][0][[1,2,3], [4,5]]none4
9End of execution[[1,2,3], [4,5]]nonenone
💡 All arrays allocated and accessed; execution ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
jaggednull[null, null][array, null][[1,2,3], null][[1,2,3], array][[1,2,3], [4,5]][[1,2,3], [4,5]][[1,2,3], [4,5]]
jagged[0]N/AN/A[0,0,0][1,2,3][1,2,3][1,2,3][1,2,3][1,2,3]
jagged[1]N/AN/AN/AN/A[0,0][4,5][4,5][4,5]
valN/AN/AN/AN/AN/AN/A22
Key Moments - 3 Insights
Why do we need to allocate each inner array separately?
Because jagged arrays are arrays of arrays, each inner array can have different sizes and must be created individually as shown in steps 3 and 5.
What happens if we try to access an inner array before allocating it?
It will cause a runtime error because the inner array is null until allocated, as seen in step 2 where inner arrays are null.
How do we access elements in a jagged array?
We use two indices: first for the outer array (row), then for the inner array (column), like jagged[0][1] in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of jagged[1][1] after step 6?
A5
B4
C0
Dnull
💡 Hint
Check the 'Inner Array Allocated' column at step 6 where jagged[1] is assigned [4,5].
At which step is the outer array allocated?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column for allocation of the outer array.
If jagged[0] was not allocated, what would happen when accessing jagged[0][1]?
AIt would return null
BIt would return 0
CIt would cause a runtime error
DIt would allocate the inner array automatically
💡 Hint
Refer to the key moment about accessing inner arrays before allocation.
Concept Snapshot
Jagged arrays are arrays of arrays with different lengths.
Declare outer array first: int[][] arr = new int[rows][];
Allocate each inner array separately: arr[0] = new int[length];
Access elements with two indices: arr[row][col].
Useful for non-rectangular data structures.
Full Transcript
Jagged arrays in C# are arrays where each element is itself an array, allowing different lengths for each inner array. First, you declare the outer array with a fixed number of rows. Then, you allocate each inner array separately, which can have different sizes. You access elements using two indices: the first for the outer array (row), and the second for the inner array (column). This allows flexible data structures that are not rectangular. The execution trace shows declaring the jagged array, allocating the outer array, allocating and assigning inner arrays, and accessing elements safely after allocation.

Practice

(1/5)
1. What is a jagged array in C#?
easy
A. An array of arrays where inner arrays can have different lengths
B. A two-dimensional array with fixed rows and columns
C. An array that stores only integers
D. A single-dimensional array with fixed size

Solution

  1. Step 1: Understand array types in C#

    A jagged array is an array whose elements are arrays themselves, allowing different lengths for each inner array.
  2. Step 2: Compare with other array types

    Unlike two-dimensional arrays, jagged arrays do not require all inner arrays to have the same size.
  3. Final Answer:

    An array of arrays where inner arrays can have different lengths -> Option A
  4. Quick Check:

    Jagged array = array of arrays with varying lengths [OK]
Hint: Jagged arrays hold arrays inside arrays with different sizes [OK]
Common Mistakes:
  • Confusing jagged arrays with 2D arrays
  • Thinking all inner arrays must be same length
  • Assuming jagged arrays store only one data type
2. Which of the following is the correct way to declare a jagged array of integers in C#?
easy
A. int[][] jaggedArray = new int[3][];
B. int[] jaggedArray = new int[3,3];
C. int[] jaggedArray = new int[3][];
D. int jaggedArray[][] = new int[3][];

Solution

  1. Step 1: Recall jagged array declaration syntax

    Jagged arrays use double square brackets: int[][] to declare an array of arrays.
  2. Step 2: Check each option

    int[][] jaggedArray = new int[3][]; correctly declares a jagged array with 3 inner arrays uninitialized. int[] jaggedArray = new int[3,3]; is a 2D array, not jagged. int[] jaggedArray = new int[3][]; has wrong type on left side. int jaggedArray[][] = new int[3][]; has invalid syntax.
  3. Final Answer:

    int[][] jaggedArray = new int[3][]; -> Option A
  4. Quick Check:

    Use int[][] for jagged arrays [OK]
Hint: Use double brackets [][] to declare jagged arrays [OK]
Common Mistakes:
  • Using single brackets for jagged arrays
  • Confusing jagged arrays with 2D arrays
  • Incorrect syntax like int jaggedArray[][]
3. What will be the output of the following C# code?
int[][] jagged = new int[2][];
jagged[0] = new int[] {1, 2};
jagged[1] = new int[] {3, 4, 5};
Console.WriteLine(jagged[1][2]);
medium
A. 3
B. IndexOutOfRangeException
C. 2
D. 5

Solution

  1. Step 1: Understand jagged array initialization

    The jagged array has two inner arrays: jagged[0] = {1, 2} and jagged[1] = {3, 4, 5}.
  2. Step 2: Access jagged[1][2]

    jagged[1] is {3, 4, 5}, so index 2 is the third element, which is 5.
  3. Final Answer:

    5 -> Option D
  4. Quick Check:

    jagged[1][2] = 5 [OK]
Hint: Remember zero-based indexing for inner arrays [OK]
Common Mistakes:
  • Confusing indices and picking wrong element
  • Assuming all inner arrays have same length
  • Expecting runtime error for valid index
4. Identify the error in the following jagged array code:
int[][] jagged = new int[2][];
jagged[0] = new int[2] {1, 2};
Console.WriteLine(jagged[1][0]);
medium
A. Cannot use Console.WriteLine with jagged arrays
B. jagged[1] is not initialized before accessing
C. Inner arrays must have same length
D. Syntax error in jagged array declaration

Solution

  1. Step 1: Check initialization of inner arrays

    jagged[0] is initialized, but jagged[1] is not assigned any array.
  2. Step 2: Accessing uninitialized inner array

    Accessing jagged[1][0] causes a NullReferenceException because jagged[1] is null.
  3. Final Answer:

    jagged[1] is not initialized before accessing -> Option B
  4. Quick Check:

    Initialize all inner arrays before access [OK]
Hint: Always initialize each inner array before use [OK]
Common Mistakes:
  • Assuming inner arrays auto-initialize
  • Ignoring NullReferenceException possibility
  • Thinking jagged arrays require equal inner lengths
5. Given a jagged array int[][] data where each inner array represents scores of students in different subjects, how would you calculate the total sum of all scores?
hard
A. Use a single loop over data.Length and sum data[i][0] only
B. Sum only the first inner array elements
C. Use nested loops to iterate each inner array and sum all elements
D. Use jaggedArray.Sum() directly without loops

Solution

  1. Step 1: Understand jagged array structure

    Each inner array has varying length representing scores per student.
  2. Step 2: Use nested loops to sum all elements

    Outer loop iterates each inner array; inner loop sums elements inside each inner array.
  3. Step 3: Avoid incorrect approaches

    Summing only first inner array or first element misses data; jaggedArray.Sum() won't work directly as it's an array of arrays.
  4. Final Answer:

    Use nested loops to iterate each inner array and sum all elements -> Option C
  5. Quick Check:

    Nested loops sum all jagged array elements [OK]
Hint: Sum jagged arrays with nested loops over all inner arrays [OK]
Common Mistakes:
  • Summing only one inner array
  • Using single loop ignoring inner arrays
  • Expecting built-in sum on jagged array
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard