An array is an index-based data structure, which means every element is referred to 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 an implementation of an array class and the methods in the class – add, remove, search, and traversal.

array class diagram

Most programming languages such as C, C++, and Java have built-in support for arrays. Some languages such as Python don’t have a 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 the array works under the hood.

Map of Array class implementations

you are here

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

Table of Content


Define an Array class and add elements

First, you define an Array class. Add an element at the end, or the given index. In Java, you need to check whether you have reached the max size.

Java

Javascript

Python

Doodle

array add


Delete an 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 the array class in Java, JavaScript and Python code
Data Structures Illustrated Python Book


You may also like