Arrays Introduction

Arrays Introduction

Array is a basic data structure used to store a list of elements. Each element is accessed by its index, starting from zero.

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndices": [0,1,2,3,4], "specialIndices": [] }

In this example, [10, 20, 30, 40, 50] are the array elements, and 0, 1, 2, 3, 4 are the respective index values.

Definition of an Array

An array is a linear data structure that stores elements in contiguous memory locations. Each element can be accessed directly using its index.

Arrays are ideal for situations where fast retrieval and processing of data is required.

Key Features of Arrays

1. Indexed Access

Each element in an array can be accessed using its index.

index starts from 0.

Consider the following array:

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndices": [0,1,2,3,4], "specialIndices": [] }
  • index of first element 10 is 0.
  • index of second element 20 is 1.
  • index of third element 30 is 2.
  • index of fourth element 40 is 3.
  • index of fifth element 50 is 4.

For example, array[2] gives you the third element: 30.

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndices": [0,1,3,4], "highlightIndicesGreen": [2] }

2. Fixed or Dynamic Size

In some programming languages, arrays have a fixed size and cannot be resized. In others, like high-level scripting languages, Python, JavaScript, etc., arrays can grow or shrink dynamically as elements are added or removed.

Array with elements appended:

Consider the following array:

{ "array": [10, 20, 30, 40, 50], "showIndices": true, "highlightIndices": [0,1,2,3,4] }

Let us add two more elements, say 60 and 70, to this array:

{ "array": [10, 20, 30, 40, 50, 60, 70], "showIndices": true, "highlightIndices": [0,1,2,3,4], "highlightIndicesBlue": [5, 6] }

Or, if you remove the last two elements from the original array, then the resulting array becomes the followign with size reduced by two.

{ "array": [10, 20, 30], "highlightIndices": [0,1,2] }

3. Contiguous Memory Allocation

Arrays store elements in contiguous memory blocks, which allows fast access and better CPU cache performance. While this is handled internally, it’s a key reason arrays are fast and reliable.

4. Efficient Traversal

Since arrays use sequential memory and indexed access, traversing the array using loops is highly efficient. For example, looping from the first to the last index allows visiting every element once.

Common Use Cases of Arrays

Common Use Cases of Arrays

1. Storing Lists of Data

Arrays are used for storing ordered collections of data such as numbers, characters, or objects. Some of the examples are tracking student grades, storing temperature readings, or listing items in a shopping cart.

2. Matrix Representations

Arrays can be used for representing matrices — grids of rows and columns.

3. Algorithm Design

Many core computer science algorithms rely on arrays due to their predictable structure and efficient access time. Sorting algorithms like Bubble Sort, Quick Sort, or Merge Sort use arrays as the fundamental data structure. Searching algorithms such as Linear Search or Binary Search also work directly with arrays.

4. Buffers and Queues

Arrays act as the underlying structure for many abstract data types such as stacks, queues, deques, and circular buffers.

Check your understanding

What is the primary benefit of using arrays in programming?
Choose one answer. You can try again.

Answer and explanation

They allow random memory access.

Arrays allow direct access to any element using its index, which makes them incredibly fast for lookups. This is called 'random access' — you don’t need to scan from the beginning to reach a specific item.

Arrays store elements in non-contiguous memory locations.
Choose one answer. You can try again.

Answer and explanation

False

Arrays are stored in contiguous (continuous) blocks of memory. This makes access predictable and efficient because the computer can calculate exactly where each element is located based on the starting point and the index.

Which of the following are valid features of arrays?
Choose one answer. You can try again.

Answer and explanation

Fixed or dynamic size; Sequential memory layout; Efficient indexed access

Arrays can be fixed-size (like in C) or dynamic (like in Python). They are stored sequentially in memory, allowing for fast indexed access. However, they can store many types—not just strings—depending on the language.

In an array [10, 20, 30, 40, 50], what is the value at index 2?
Choose one answer. You can try again.

Answer and explanation

30

In most programming languages, array indexing starts at 0. So index 0 is 10, index 1 is 20, and index 2 is 30. That’s why the correct answer is 30.

Arrays are often used to implement data structures like stacks and queues.
Choose one answer. You can try again.

Answer and explanation

True

Stacks and queues are built on arrays because arrays provide efficient ways to add and remove items from ends or specific positions. Their predictable structure makes them ideal for such use cases.

In which of the following applications are arrays commonly used?
Choose one answer. You can try again.

Answer and explanation

Matrix representations in graphics; Sensor data buffers

Arrays are perfect for handling continuous data like graphics (where images are stored as pixel matrices) and sensor data (which comes in streams and needs quick access and updates).