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.
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.
C++
Java
Typescript
Javascript
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.
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.
C++
Java
Typescript
Javascript
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.
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.
C++
Java
Typescript
Javascript
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.
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.
C++
Java
Typescript
Javascript
Python