1D Arrays
A one-dimensional (1D) array is a collection of items of the same data type.
- To declare an array in pseudocode, use the syntax:
DECLARE arrayName[n] OF dataType
- To declare an array in Python, use the syntax:
array_name = [0] * n
- In Java, use the syntax:
dataType[] arrayName = new dataType[n];
- In Visual Basic, use the syntax:
Dim arrayName(n) As dataType
To access elements in a 1D array, use the index, which can start at 0 or 1.
2D Arrays
A two-dimensional (2D) array is an array of arrays, creating a grid-like structure.
- To declare a 2D array in pseudocode using the syntax:
DECLARE arrayName[n][m] OF dataType
- In Python, use the syntax:
array_name = [[0] * m for _ in range(n)]
- In Java, use the syntax:
dataType[][] arrayName = new dataType[n][m];
- In Visual Basic, use the syntax:
Dim arrayName(n, m) As dataType
To access elements in a 2D array, use two indices: the row index and the column index.
The syntax for accessing elements is similar across languages, using square brackets:
[i]
for 1D arrays,- and
[i][j]
for 2D arrays.