Declare and use one-dimensional and two-dimensional arrays

Resources | Subject Notes | Computer Science

Declaring and Using One-Dimensional and Two-Dimensional Arrays

Introduction

Arrays are fundamental data structures in computer science used to store a collection of items of the same data type. In this topic, we will learn how to declare and use one-dimensional (1D) and two-dimensional (2D) arrays in a programming language like Python. Arrays provide an efficient way to manage and access multiple related data points using a single variable name and an index.

One-Dimensional Arrays

A one-dimensional array is a linear collection of elements, accessed using a single index. The elements are stored contiguously in memory.

Declaration

In Python, a one-dimensional array is typically implemented using a list. We can declare a list of a specific size with all elements initialized to a default value (e.g., 0) or declare an empty list and then populate it later.

Example (Python):


# Declare an array of 5 integers, initialized to 0
my_array = [0] * 5

# Declare an empty array
another_array = []

Accessing Elements

Elements in a 1D array are accessed using their index, starting from 0. The index represents the position of the element within the array.

Example (Python):


my_array[0]  # Accesses the first element
my_array[2]  # Accesses the third element

Iteration

We can iterate through the elements of a 1D array using a loop.

Example (Python):


for i in range(len(my_array)):
    print(my_array[i])

Two-Dimensional Arrays

A two-dimensional array is an array of arrays, representing a grid or table of elements. It is accessed using two indices: one for the row and one for the column.

Declaration

In Python, a 2D array is typically implemented as a list of lists.

Example (Python):


# Declare a 2D array with 3 rows and 4 columns, initialized to 0
my_2d_array = [[0] * 4 for _ in range(3)]

# Alternatively, you can initialize it row by row
another_2d_array = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

Accessing Elements

Elements in a 2D array are accessed using their row and column indices.

Example (Python):


my_2d_array[0][0]  # Accesses the element in the first row and first column
my_2d_array[1][2]  # Accesses the element in the second row and third column

Iteration

We can iterate through the elements of a 2D array using nested loops.

Example (Python):


for row in range(len(my_2d_array)):
    for col in range(len(my_2d_array[row])):
        print(my_2d_array[row][col])

Example Program

The following Python program demonstrates the declaration, initialization, and access of elements in both 1D and 2D arrays.


# One-dimensional array example
one_d_array = [10, 20, 30, 40, 50]
print("One-dimensional array:")
for i in range(len(one_d_array)):
    print(f"Element at index {i}: {one_d_array[i]}")

# Two-dimensional array example
two_d_array = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
print("\nTwo-dimensional array:")
for row in range(len(two_d_array)):
    for col in range(len(two_d_array[row])):
        print(f"Element at row {row}, column {col}: {two_d_array[row][col]}")

Summary

Arrays are essential for storing and managing collections of data. Understanding how to declare, initialize, and access elements in both one-dimensional and two-dimensional arrays is crucial for various programming tasks.

Table: Array Indexing

Array Type Indexing Example
One-Dimensional Starts at 0 and goes up to the length of the array minus 1. my_array[2] accesses the third element.
Two-Dimensional Requires two indices: one for the row (starting from 0) and one for the column (starting from 0). my_2d_array[1][0] accesses the element in the second row and first column.