9.2 Arrays, Lists, and Two-Dimensional Arrays

Key Takeaways

  • An array of length n has valid indexes 0 through n − 1 when the first element is at index 0; index n is out of bounds.
  • In ETS pseudocode, arrays are declared as int[ ] a ← {1, 2, 3} or int b[0..2] ← {1, 2, 3}, and a two-dimensional array as int[ ][ ] c.
  • Initialize a running minimum or maximum to the first element, not to 0, or all-positive or all-negative data give wrong answers.
  • A two-dimensional array is accessed as grid[row][col]; nested loops over R rows and C columns visit R × C cells.
  • Dynamic lists (such as Java's ArrayList) append in amortized O(1) time but need O(n) time to insert or remove at the front, because elements must shift.
Last updated: September 2026

Where arrays appear on the test

ETS lists arrays/lists among the core data types (Section 7.1), asks you to trace code when arrays are passed to procedures (Section 9.1), and uses arrays in many algorithm questions: finding a maximum, sorting, searching, and counting. The notation table includes both int[ ] and int[ ][ ].

One-dimensional arrays

An array stores a fixed number of values of one type under one name. Individual elements are reached by index.

int[ ] a ← {90, 85, 77, 92}      // length 4
int b[0..2] ← {1, 2, 3}          // declaration that shows the index range
print a[0]                       // 90
a[2] ← a[2] + 5                  // a is now {90, 85, 82, 92}

With the first element at index 0, an array of length n has indexes 0 through n − 1. Using index n, or −1, is an out-of-bounds error at run time.

Arrays are stored in consecutive memory locations. That is why any element can be reached directly: its address is base address + index × element size. With a base address of 2048 and 4-byte integers, a[7] is at 2048 + 7 × 4 = 2076. Access by index is O(1).

Traversal patterns

Most array code is one of a handful of loops.

Sum and average

int sum ← 0
for ( int i ← 0; i < n; i ← i + 1 )
    sum ← sum + a[i]
end for
double avg ← sum / n        // watch for integer division; see Section 7.1

Minimum (or maximum)

int min ← a[0]                     // start with a real element
for ( int i ← 1; i < n; i ← i + 1 )
    if ( a[i] < min )
        min ← a[i]
    end if
end for

Starting min at 0 is a classic bug. If every value is positive, the "minimum" stays 0, which is not in the array. ETS's sample question on finding a maximum completes this pattern with the missing condition numList[i] > max.

Count matches

int count ← 0
for ( int i ← 0; i < n; i ← i + 1 )
    if ( a[i] ≥ 70 )
        count ← count + 1
    end if
end for

Reverse in place: swap a[i] with a[n - 1 - i] for i from 0 while i < n / 2. Going all the way to n would swap everything back.

Shift left (delete at index k): copy a[i + 1] into a[i] for i from k to n − 2. The last slot then holds a duplicate or unused value.

Two-dimensional arrays

A 2D array is a grid of rows and columns, accessed as grid[row][col].

          col 0   col 1   col 2   col 3
row 0  [   10      12      14      16  ]
row 1  [   20      22      24      26  ]
row 2  [   30      32      34      36  ]

Here grid[1][2] is 24. There are 3 rows and 4 columns.

Row-by-row traversal (row-major)

for ( int r ← 0; r < numRows; r ← r + 1 )
    for ( int c ← 0; c < numCols; c ← c + 1 )
        print grid[r][c]      // print a space after each value
    end for
end for

The inner statement runs numRows × numCols times, 12 in the example. Swapping the loops traverses column by column instead.

Useful 2D patterns

TaskHow
Row sumsFor each r, sum grid[r][c] over all c
Column sumsFor each c, sum grid[r][c] over all r (outer loop over columns)
Main diagonal (square grid)One loop: grid[i][i]
Neighbors of a cellCheck r − 1, r + 1, c − 1, and c + 1, staying within bounds

C and C++ store 2D arrays row-major in memory: all of row 0, then row 1, and so on. The cell grid[r][c] is at base + (r × numCols + c) × size. Java and Python build 2D structures as arrays (or lists) of rows, which are also naturally traversed row by row.

Dynamic lists

A fixed-size array cannot grow. Most languages provide a dynamic list, such as Java's ArrayList, Python's list, or C++'s vector. It stores its elements in an internal array that has spare room.

  • Size: how many elements are stored. Capacity: how many fit before the internal array must grow.
  • Appending when capacity remains is O(1). When the array is full, the list allocates a larger array (grown by a constant factor; Java's ArrayList grows by about 1.5×) and copies everything over, which is O(n) for that one append. Because growth is geometric, the copying averages out to amortized O(1) per append.
  • Inserting or removing at index k shifts every element after k. At the front, that is O(n).
  • Access by index remains O(1).
OperationFixed arrayDynamic list
Read or write by indexO(1)O(1)
Append at endNot possible when fullO(1) amortized
Insert or remove at frontO(n) shifting, or not possibleO(n)
Search unsortedO(n)O(n)

Arrays and procedures

An array passed to a procedure is shared, not copied (Section 9.1). A procedure that sorts, fills, or modifies an array's elements changes the caller's array. A procedure that needs to leave the original alone must make its own copy first.

Test Your Knowledge

An array of 4-byte integers begins at memory address 2048, and its first element is at index 0. What is the address of the element at index 7?

A
B
C
D
Test Your Knowledge

A 3-row, 4-column array holds grid[r][c] = 4 * r + c. What value does this segment store in total?

int total ← 0
for ( int r ← 0; r < 3; r ← r + 1 )
    for ( int c ← 0; c < 4; c ← c + 1 )
        if ( r == c )
            total ← total + grid[r][c]
        end if
    end for
end for

A
B
C
D
Test Your Knowledge

This segment is intended to find the smallest value in an array a of length n.

int min ← 0
for ( int i ← 0; i < n; i ← i + 1 )
    if ( a[i] < min )
        min ← a[i]
    end if
end for
Which array would reveal that the segment is incorrect?

A
B
C
D
Test Your Knowledge

A dynamic list (such as a Java ArrayList) currently holds n elements. What is the worst-case time to insert a new element at index 0?

A
B
C
D