An array is an index-based data structure, which means every element is referred by an index.  Array data are stored in sequential memory. The index runs from 0 to the array size minus one. The size of an array should be specified when initializing an array in Java. Here is implementation of array operations – add, remove, search and traversal.

array diagram

Most programming languages such as C, C++ and Java have built-in support for array. Some languages such as Python doesn’t have built-in array, but provide external libraries such as array module or NumPy array to work with. Most time you don’t need to implement your own class. The implementation itself will help you understand how array works under the hood.

Map of Array implementations

you are here

Part 1 – Array implementation
Part 2 – Sorted array implementation
Part 3 – Matrix 2d array implementation

Table of Content


Add element in Array

Add element at the end, or at the given index. In Java, you need check whether you have reached the max size.

Java

Javascript

Python

Doodle

array add


Delete element

Delete the element by key or by index. You need to move the following elements to their preceding position.

Java

Javascript

Python

Doodle

array delete


Linear search

Starting from the index 0, compare each element in the array with the key . Return the first matched element’s index. If the key is not found, return -1.

Java

Javascript

Python

Doodle

array search


Print elements

Print all elements in the array from index 0 to the last.

Java

Javascript

Python

Doodle

array search


Free download

Download Java, JavaScript and Python code
Data Structures Illustrated Python Book

When you delete an element in an array, why do you move the right elements left to fill the hole?

array implementation

The data in an array are leftmost. There are no holes (missing data) between them. If holes were allowed, all the algorithms of arrays would become more complicated. The only empty spots are at the end.