Array Data Structure - Explained with Examples

Depending on the type, arrays can either be a linear sequence of objects like students in an assembly row or a rectangular arrangement of objects in rows and columns like we have on a chessboard

Array Data Structure - Explained with Examples

In Computer Sciences, Data structures are frameworks to store, organize and process data so that a particular value can be easily located.

These frameworks/structures are of different types such as stacks and queues to name a few.

You know like the different types of containers we have in our kitchens.

An array is one such data structure.

Introduction to Array Data Structure

Arrays are a type of data structure in which elements of the same data type are sorted in contiguous memory locations. 

Arrays are among the most primitive and most important data structures in computer programming. We use arrays all the time in almost every program. 

They productively utilize the addressing logic of computers as in most modern computers and external storage devices, the memory is in the form of a one-dimensional array of words.

Table of contents: 

What are the types of array?
Advantages and limitations of array
What are the applications of array?
Array functions in C

What do Arrays look like?

Depending on the type, arrays can either be a linear sequence of objects like students in an assembly row or a rectangular arrangement of objects in rows and columns like we have on a chessboard.

Examples of array data structures
Icon credits: Hosting by DinosoftLab, Table by Atif Arshad from

Think of it. We have two bishops, rooks, and knights each on a chessboard but we’re able to locate each individual because of their positions (index).

Basic Terms of An Array Data Structure

In arrays, an element refers to a particular item that is stored.

Each element carries an index, a location with respect to a base value.

The base value is the memory location of the first element of the array.

We simply add offsets to this value which makes it easier for us to use the reference and identify items.

Array length – The number of elements an array can store is defined as the length of the array. It is the total space allocated in memory while declaring an array.

Why do we use arrays?

Imagine you’re developing an application for a t-shirt shop and you have three different colors – black, white, and orange.

How would you define the colors in your application?

In Java, You might follow this:

String black = "black"
String white = "white"
String orange = "orange"

But what if you contain the three elements in one single section where you can index or assign a position to each element.

Wouldn’t that be easier to access?

That’s where we use an array data structure.

The above scenario can be defined in an array as:

String[ ] colours = ["Black", "White", "Orange"] ;

Now, you can access each element as –

System.out.println (colours [1] ) ; // prints White.

Yes, did you think it would print “black” as black is first in the sequence?

Indexing is different in Java and most other programming languages. For that case, you should remember that counting starts from 0 and not 1.

Types of Array Data Structure

One-dimensional array

It is a linear data structure in which elements are stored in adjacent memory locations.

(For example, there are multiple coaches connected to each other in a train.)

1D Array data structure image

The formula for finding the memory location of a particular element ‘N’  is:



Address – memory location of the Nth element

WORDLENGTH – number of bytes required to store one element. It depends upon the data type – a character requires 1 byte and an integer value needs 2 bytes.

LOWERBOUND – index of the first element of the array

BASE ADDRESS – Address of the first element of the array.

Declaring a 1D Array

Usually, an array is declared by mentioning the type name, followed by the variable name and the array’s size at the end.

type arrayName [ arraySize ];

So, a linear array consisting of 10 integers will be declared as-

Int Numbers [10]

Two-dimensional Array

As the name suggests, a two-dimensional array is made up of rows and columns. The above-mentioned chessboard is a perfect example of 2D arrays.

2D array data structure image

We can calculate the size of a 2D array data structure by multiplying the number of rows and columns.

Similar to how we define a cell in Excel or address a seat in a multiplex, the position of elements in 2D arrays is defined by using two subscripts – the row number and the column number.

Declaring a 2D Array

We’ll follow the same process as a 1D array except we’ll add another dimension to the size of the array. 

So, declaring a 2D array with 10 rows and 8 columns will be as follows-

Int Numbers [10][8]

Three-dimensional Array

A 3D array is simply a collection of 2D arrays.

3D Array data structure Image

Just like we studied in maths, we add height or depth to the two-dimensional array to get a three-dimensional array.

So, the maximum number of elements in a 3D array of dimensions –  [5*4*3] = 60

Let’s understand this better

Suppose, you have 8 cards/bricks and you have to place them in 3 different setups

1.) You can lay all 8 of them flat in a row (1D array)

2.) You can divide them into groups of 4 each and make a new column right beside them.

3.) Or you can place 4 at the bottom in 2 rows and 2 columns and then place the other 4 over them to add another dimension making it a 3D array.

3D arrays are recognized using three subscripts –  row size, column size, and block size. 

      Masai School provides courses in web/software development and helps students get placed in high-end software jobs. 

      Check out the Full Stack Web Development course and master programming at no initial fees with Income Share Agreement.

Advantages of Arrays

• In arrays, we can store a large number of elements that can be defined by a single line of code rather than naming each variable separately. Thus, it helps in the readability and reusability of the code.

• We can access any random element in arrays by using the index number. Thus, the time complexity to access any element in an array is constant, unlike stacks where one has to start from the top to access the bottom elements.

• There’s no need for the allocation of extra memory as arrays distribute memory in contiguous memory locations. This prevents memory overflow. Moreover, we can also allocate memory dynamically in an array.

• We can easily create new subarrays within a given array.

• Since arrays are the most basic data structures, they can be used to implement other data structures such as stacks, queues, graphs, linked lists, etc.

• Functions such as finding maximum and minimum values or implementing searching and sorting algorithms are easier to perform in an array. 

Limitations of Arrays

  • Shifting – Insertion and deletion processes are tricky in arrays. If we want to keep the elements ordered and insert a new element in its correct position or remove it, then we’ll need to move other elements (half of the elements on average).
  •  Fixed Size – Arrays are static structures. Once the size of an array is declared, it cannot be increased or decreased.

We must know the correct number of elements in the correct order before allocating memory otherwise there would be either shortage or wastage of memory.

  • Inability to store different data types

As we’ve discussed earlier, arrays can only contain homogeneous elements.

It’s fine as long as you keep all eggs in the carton, one of them hatches and it’s not an array anymore.

So, an integer array will only contain integers, similarly, a character array will only have characters.

Exception – An Array in Javascript can contain different types of elements including strings, integers, or even other arrays. So, we can store a number in the first position, a string in the second, and so on as shown in the table below.

Value5Masai School-17[“Masai, “Coding”]

Applications of Array Data Structure

Let’s say we have to calculate the average distance traveled by a person in 5 days. We’d simply add the variables of each day and find their average.

But if we have to find the average of a whole year,  it would be difficult to keep a track of so many variables, perform add function on them and get the desired result.

That’s where Arrays come into the picture.

By now, you must have understood how we use arrays in real life.

Let’s now look at some applications of arrays in programming-

1.) If we have to store a large list of values of the same data type under a single variable name, we use arrays.

2.) Two-dimensional Arrays are used for performing matrix operations. Many databases include 1D and 2D arrays whose elements are records. 

3.) Arrays help in implementing CPU scheduling algorithms. They can store the list of processes that need to be scheduled and thus have a significant role in maintaining the efficiency of the operating system.

4.) As discussed earlier, arrays also help in implementing other data structures such as stacks, queues, hash tables, etc.

How are arrays implemented in different Programming languages?

Though the concept is the same for all data structures, programming languages implement them differently.

So is the case with Arrays.

Let’s see what commands are used to define or declare an array in different languages –

In C – type arrayName [ arraySize ];

So, if we have a linear row of 10 numbers, we’d declare it as-

int number[10] 

Suppose we have to calculate the number of people in 6 different locations and perform basic operations on the data like finding maximum or minimum, this is how we’d define it in different programming languages.

Programming LanguageArray Implementation
C++int number[] = {50, 75, 90, 136, 80, 60};
Javaint[] number = {50, 75, 90, 136, 80, 60};
Javascriptvar number = [50, 75, 90, 136, 80, 60];
Swiftvar number: [50, 75, 90, 136, 80, 60];

Array Functions in C


Traversing an array means visiting every single element of the array once in order to check the data so we can use it for different purposes. It starts from the first element and goes till the last one.

Input: arr[] = {3, 5, 7, 2, -1}

Output: 3, 5, 7, 2, -1


Insertion refers to adding a new element in an array. We have to specify the new element and its position before inserting it. 

Insert function in arrays

Since we know that arrays have a fixed size and cannot be extended, we can’t add a new element if the array is already full.

Accessing an Element in Arrays

We can use array index to access any random element. Indexing starts with ‘0’, therefore the first element would be arr [0] in an array of n elements.

To access the nth element of an array element, we’ll use the index [n-1]

For example:

For the 7th element, we’ll use the index arr[6] to get the desired output.

Search Algorithms in Array Data Structure

Let’s say we have to search a given value ‘x’ in an array arr[] of ‘n’  elements

We can perform a linear search

• We’ll start matching ‘x’ with each element in the sequence arr[]

• If ‘x’ matches with an element, it returns the index i.e. the location of the element.

• If ‘x’ doesn’t match, it returns -1

Linear search algorithm demonstration

There’s another search method called “binary search”.

It works by comparing the required value to the middle element of the list. Further, it keeps dividing in half the section of the list that could contain the value, until we get a single location.

Note- Binary Search only works when you have a sorted array.

Sorting Algorithms in Array Data Structure

We use the sorting function to sort the elements of an array into a fixed order i.e either ascending or descending. 

Below are the different types of sorting algorithms we can use-

  • Bubble Sort
  • Selection Sort
  • Merge Sort
  • Insertion Sort
  • Quick Sort
  • Heap Sort


We hope that you were able to understand most things about array data structure. Learning to use arrays is an integral part of knowing what happens backstage in programming and also plays an important role in learning simple programming languages like C.

If you’re a beginner and you want to learn everything about data structures and algorithms from scratch, do refer to this well-written, in-depth article on ‘DSA – Explained with Examples’.

However, if you’re serious about programming and want to build your career as a software engineer, learning from random sources won’t be of much help. You wouldn’t be able to organize all your learnings in one place and put them into practice. You wouldn’t have enough hard skills to crack software developer roles. 

That’s why we recommend you to go for structured learning with an online/offline course in software development. There are hundreds of such courses on the internet, choose the one that makes sense to you and start learning.

Masai School teaches web/software development to students from different backgrounds at zero upfront payment and gets them placed in high-end software jobs with a minimum package of 5 LPA. 

If you are unable to get a job, you don’t have to pay a single penny for the course. Sounds like a win-win situation? It indeed is.

Check out our courses and learn more about how the fee structure works before you make a decision.

Our last 3 batches have had a 100% placement record and we’re only looking to continue on that path for our upcoming batches.

Cheers & Happy Coding!