Use arrays to store multiple values of the same data type

Resources | Subject Notes | Computer Science

Arrays in Computer Science

Objective: Use arrays to store multiple values of the same data type

An array is a fundamental data structure in computer science used to store a collection of items of the same data type. These items are stored in contiguous memory locations, allowing for efficient access. Arrays are a core concept for managing and processing sets of data.

What is an Array?

Think of an array as a row of boxes, where each box can hold a value of the same type (e.g., integers, characters, floating-point numbers). Each box has a unique index, which we use to access the value stored in that box.

Array Declaration and Initialization

In most programming languages, arrays are declared with a specific data type and a size (number of elements). Here's how you might declare and initialize an array in a typical syntax:


# Python example
my_array = [10, 20, 30, 40, 50] 
# This declares an array named 'my_array' containing 5 integer elements.

// Java example
int[] myArray = new int[5];
// This declares an integer array named 'myArray' with a size of 5.

Accessing Array Elements

Elements in an array are accessed using their index. Array indices typically start from 0. So, the first element is at index 0, the second at index 1, and so on.

The syntax for accessing an element varies slightly between languages:

  • Python: `my_array[0]` (accesses the first element)
  • Java: `myArray[0]` (accesses the first element)

Array Operations

Common operations performed on arrays include:

  • Accessing elements: Retrieving a specific element using its index.
  • Modifying elements: Changing the value of an element at a given index.
  • Iteration: Processing each element in the array, often using loops.
  • Searching: Finding a specific element within the array.
  • Sorting: Arranging the elements in a specific order.

Example: Iterating Through an Array

Here's a simple example of how to iterate through an array using a loop:


# Python example
my_array = [1, 2, 3, 4, 5]
for element in my_array:
    print(element)

// Java example
int[] myArray = {1, 2, 3, 4, 5};
for (int i = 0; i < myArray.length; i++) {
    System.out.println(myArray[i]);
}

Table: Array Operations and Syntax

Operation Syntax (Python) Syntax (Java)
Access Element my_array[index] myArray[index]
Modify Element my_array[index] = new_value myArray[index] = new_value
Access Length len(my_array) myArray.length

Applications of Arrays

Arrays are used extensively in computer science for various applications, including:

  • Storing lists of student names
  • Representing pixel data in an image
  • Storing sensor readings
  • Implementing stacks and queues
  • Performing mathematical computations
Suggested diagram: A visual representation of an array with labeled indices and elements.

Further Considerations

When working with arrays, it's important to be mindful of array bounds. Accessing an element outside the valid range of indices can lead to errors. In some languages, this will cause a runtime error. Understanding array bounds is crucial for writing robust and reliable code.