Overview of supported operations
Now that we know the logical representation of an array, let's examine how to create, access, modify, 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. A fixed-size array cannot be resized after creation, though its individual elements can still be modified, 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
int numbers[5]?In C and C++, whatever bytes were already sitting in that memory block. These leftover bytes are called garbage values, and reading them is a bug. Java instead fills every new array with
0s, and in JavaScript and Python a list only ever holds the values we put into it.Accessing elements in an array
An array is just a collection of data items stored in contiguous memory. This contiguous 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 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.
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
numbers[5]?Valid indices for an array of size
5 are 0 to 4. Accessing anything outside this range is called an out-of-bounds access. C and C++ compute the address anyway and read whatever memory lives there, returning garbage or crashing the program. Java and Python check the index first and stop the program with an error, while JavaScript returns undefined. This is why the array's size must be known to traverse it safely.Modifying elements in an array
Elements in an array can be modified in place, just like values held by variables. 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 standard way to search for a value in an unsorted 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 (the array's size, the number of elements it holds). For lower-level languages like C/C++, however, the programmer needs to keep track of the array's size. Beyond the index-based loop described above, the code below also shows the other loop forms each language provides, including a reverse traversal that visits elements from the last index (size - 1) down to 0.
C++
Java
Typescript
Javascript
Python