Data Structures and Algorithm: Lets Talk Arrays

Oindrila
6 min readSep 1, 2023

--

In the previous two articles we had a brief overview on Data Structures and Algorithms. Today we will be discussing Arrays. Also I’ll be using Java to explain the basic syntax of the data structures.

Disclaimer !

Before you proceed!

Note that this series can be considered more of a reference than a full-on tutorial, I am no expert but in here I tried to explain the concepts from my point of view and from what I’ve learned. The reader can consider this as a referring point for a better understanding.

In my previous article we’ve seen a very generic and simple overview of what Data Structures and Algorithms are. In this article I want to talk about Arrays.

Series Content:

  1. Overview of Data Structures(here).
  2. Overview Of Algorithms(here).
  3. Let’s talk Arrays(here).

ARRAYS

Arrays are the most common data structures used in storing data. The word array comes from the Old French word ‘arraier’ which means to put in order.

An array is thus an ordered collection of elements of the same data type. Lets find an easy way to figure this out. We’ll go with the classic library book example…..or wait..let’s try something else. From my previous article you’ve probably realised that I love noodles. We’ll take that as an example. Say that we have just ordered a box of instant noodles, they’re of different categories and from different manufactureres, but they’re all noodle packets. Now we have three different types of noodles there, we have instant Ramen, we have Cup Noodles and we have normal instant noodles.

What happens when we want to eat it?Say we’re craving an instant Ramen noodles(again in the middle of the night 👀). We search the box for the instant noodles that we want. In this scenario, all the noodles are mixed together, so we’re going to need to pick up each packet and see which one is the Ramen noodles that we want and then make that. And every time we crave a specific noodles, we’ll need to repeatedly search the entire box, go through all the different types of noodles until we find the one we want.

Instead, what if we arranged it in our kitchen cupboards, in terms of category and manufacturers. Like the first stack will contain only Ramen packets, first from manufacturer A and then from Manufacturer B, similarly the next stack will contain only the Instant noodles which are NOT Ramen by category and manufacturer, and the last stack will contain only the Cup Noodles, in the same manner as the other two. In this scenario, it’ll be much easier for us to pick out the kind of instant noodles we want to eat.

Ramen Stack example (totally not promoting shin ramen for free)
totally not promoting shin ramen for free! Also see how perfectly these packets are organised.

So next time when we want to eat noodles, all we need to do is go to the kitchen cupboard and select the noodles. If we want cup noodles or any other noodles , we’ll just pick out those from their respective stacks. Now imagine if we hadn’t done this, imagine if we had kept the packets in different places(say for the lack of space in the cupboard for the cup noodles), in that case, once the kitchen cupboard stack was over, we’d need to look through the other place for the cup noodles. So when we keep the stack together, not only do we have the noodles we want, we also know where the next noodle of the same or different category would be(which is the next one in the stack).

Now lets define what Arrays actually are, in a technical sense.

Arrays are data structures that store a collection of data of a single data type in a chunk of contiguous memory of a designated size.

Lets break this definition down:

a collection of data of a single data type — This means arrays store a collection of data of only a single data type, i.e., if we start with storing integers in an array, and then try to store a data of type double, it will throw an error. So it has to be

{1,2,3,4,5} ✅ // no error

and not

{1,2,3.5,4,5.5} ❎ . // causes error

This is what an array looks like.

chunk of contiguous memory — This means that the elements in the array are stored in adjacent memory blocks from each other. So when we initialise an array, we already give it a size, which holds the number of array elements that our array will contain. And the elements are stored one after the other, as shown in the image above.

How are these elements accessed?

The array is accessed using an integer called the index. The index acts as a pointer and holds the address of the elements in the array. The index value starts from 0 for the first element in the array all the way to n-1 as the last element’s index, where n is the last element. So for 5 elements, the indexing goes :

0️⃣ > 1️⃣ > 2️⃣ > 3️⃣ > 4️⃣

Internal Working of Arrays

When we initialise an Array, we assign it a specific size, which allocates a block of memory internally to hold the elements of the array. This memory block is where the elements are stored sequentially, in a linear manner. The first element is stored at the beginning of the memory block, and then sequentially the next elements are stored.

The size of the chunk of memory used for storing the array elements depends on two factors :

One : The number of elements, greater the number of elements, more the memory required.

Two: The size occupied by each element of the array. Obviously a data type requiring more space for one of its elements will require more space for all the elements. Typically an integer element requires 4 bytes of memory for each of its element. So for 5 elements the total memory would be 4*5 = 20 bytes.

Syntax to initialse an array

An array is initialised with this statement :

data_type[] variableName = new data_type[size];
int[] myArray = new int[5];

The square braces [ ] represent an array, this helps the Java compiler know that we’re trying to store an array of 5 integers and not a single integer.

We can also assign values to the initialised array, there are two ways to do it :

One : While initialising :

int[] myArray = new int[]{1,2,3,4,5};

Here we don’t need to mention the size of the array, the compiler automatically calculates it from the number of elements inside the flower braces.

Two : Using the indices

int[] myArray = new int[5];

myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
myArray[3] = 4;
myArray[4] = 5;

Accessing one of the elements in myArray would look something like this :

myArray[3] , output = 4

PROS AND CONS OF USING AN ARRAY

PROS:

  1. The elements are easy to access with the help of the index variable.
  2. Search algorithms can be applied easily in an array because of its continuous storage of memory.
  3. If we know the number of elements we need to store, we won’t waste memory assigning unnecessary space.
  4. Its easy to understand.

CONS :

  1. Arrays get assigned a fixed size during initialisation, this is a good thing when we know how many elements we want to store, but when we don’t know the number of elements to be stored, it becomes an issues.
  2. If we assign it a size to the array and then don’t end up using it, it’ll end up as a waste of space.
  3. Arrays don’t allow different data types to be stored, the data have to be of the same type as mentioned during initialisation.
  4. Adding and deleting an element would be a problem, because if the element is in the middle then the array needs to reassign the indices to the elements again.

That is all for today’s article. Hope reading this article helped you understand arrays a little better.

My Socials :
1. Github

2. LinkedIn

--

--

Oindrila

Hello, I am a Software Developer and I love to write about the technologies as I learn about them. Backend | DSA | Java | SpringBoot | LLD | HLD | Coffee