A jagged array stores a collection of rows where each row can have a different length. It is usually represented as an array of arrays.
You use it when the data is grouped by row, but each group may contain a different number of elements.
Problem
Given a jagged array , support reading and writing an element at row and column where:
and
Structure
A jagged array stores each row separately:
Rows do not need the same length.
Algorithm
Read an element:
get(A, i, j):
if i < 0 or i >= rows(A):
error "row index out of bounds"
if j < 0 or j >= length(A[i]):
error "column index out of bounds"
return A[i][j]Write an element:
set(A, i, j, x):
if i < 0 or i >= rows(A):
error "row index out of bounds"
if j < 0 or j >= length(A[i]):
error "column index out of bounds"
A[i][j] = xExample
Let
To read position :
| step | action | result |
|---|---|---|
| 1 | check row | |
| 2 | check column | |
| 3 | read value |
Return .
Correctness
The row bounds check ensures that exists. The column bounds check ensures that index exists inside that specific row. Since each row owns its own storage, reading or writing affects exactly the requested element.
Complexity
| operation | time |
|---|---|
| get | |
| set | |
| append row | amortized |
| scan all elements |
Here is the total number of stored elements across all rows.
Space usage:
where is the number of rows.
When to Use
Jagged arrays are appropriate when:
- rows have different lengths
- rectangular storage would waste space
- each row is processed independently
- adjacency lists or grouped records are needed
They are less suitable when uniform matrix operations or cache-friendly full-row scans dominate.
Implementation
class JaggedArray:
def __init__(self):
self.rows = []
def append_row(self, row):
self.rows.append(list(row))
def get(self, i, j):
if i < 0 or i >= len(self.rows):
raise IndexError("row index out of bounds")
if j < 0 or j >= len(self.rows[i]):
raise IndexError("column index out of bounds")
return self.rows[i][j]
def set(self, i, j, x):
if i < 0 or i >= len(self.rows):
raise IndexError("row index out of bounds")
if j < 0 or j >= len(self.rows[i]):
raise IndexError("column index out of bounds")
self.rows[i][j] = xtype JaggedArray[T any] struct {
rows [][]T
}
func (j *JaggedArray[T]) AppendRow(row []T) {
copied := make([]T, len(row))
copy(copied, row)
j.rows = append(j.rows, copied)
}
func (j *JaggedArray[T]) Get(i, k int) (T, bool) {
var zero T
if i < 0 || i >= len(j.rows) {
return zero, false
}
if k < 0 || k >= len(j.rows[i]) {
return zero, false
}
return j.rows[i][k], true
}
func (j *JaggedArray[T]) Set(i, k int, x T) bool {
if i < 0 || i >= len(j.rows) {
return false
}
if k < 0 || k >= len(j.rows[i]) {
return false
}
j.rows[i][k] = x
return true
}