Overview of supported operations


Now that we know the logical representation of an array, let's examine how to create, update, and traverse one. Almost all major programming languages support arrays in some form.

Creating an array

The syntax and rules for creating an array depend on the programming language. An array with a fixed size cannot be modified after creation, and all data items in an array must be of the same type.

Loading Image

Creating an array of fixed size and datatype

Higher-level programming languages like Javascript and Python inherently only provide a list instead of an array. A list behaves just like an array, but has a dynamic size and can store elements of different data types. However, the underlying machine-level implementation still uses the basic arrays as the core data structure, which has a fixed size and type.

  1. C++

  2. Java

  3. Typescript

  4. Javascript

  5. Python

Accessing elements in an array

An array is just a collection of data items stored in continuous memory. This continuous memory layout allows us to access its elements using indices. We use the subscript operator [] with an index to access data items in an array.

Why do array indices start from 0 instead of 1?

Array indices start from 0, indicating an element's relative position from the array's beginning. This makes the element at index 0 the first element, the element at index 1 the second, and so on.
Loading Image

Array elements are accessed via their indices.

Different programming languages provide different ways of accessing elements within an array. However, the underlying access mechanism is the same for all.

  1. C++

  2. Java

  3. Typescript

  4. Javascript

  5. Python

Modifying elements in an array

Elements in an array can be modified in place just like values held by variables. Just like modifying a value held in a variable, to modify a value in an array, we write the accessor array[index] on the left side of the assignment operator and the value to be assigned on the right side.

Loading Image

Array elements can be modified via their indices

Different programming languages implement the underlying operations differently. However, the underlying mechanism to update the values at the core is the same.

  1. C++

  2. Java

  3. Typescript

  4. Javascript

  5. Python

Traversing an array

Traversal is one of the most common operations performed on an array. It is the only way to search for a value in an array and is implemented by using a loop control variable as an index (starting from 0). To safely traverse an array, the size of the array should be known.

Loading Player

1 of 13

Traversing an array using a loop control variable 'index'

Higher-level programming languages have built-in functions within the array to get its length. For lower-level languages like C/C++, however, the programmer needs to keep track of the array's size.

  1. C++

  2. Java

  3. Typescript

  4. Javascript

  5. Python

Login to save progress