Home Arrays Array Data Structure

# Array Data Structure

0 comment

An array is the most basic data structure one can think of. It may seem very easy to use and in a lot of my posts we have been solving problems using arrays. However, if you are just getting started with programming this post is probably for you. I would like to cover some of the basic concepts that makes this data structure so desirable and easy to use. If you have been programming for a while now this post is probably not written for you. Read along keeping that thought in mind :)

#### Why do you need Arrays?

The best way to understand a concept is by taking up an example.

Let us assume that you are writing a code to store marks of some students in a subject ‘Physics‘. This can be done by keeping 4 variables like:

.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
marksA
marksB
marksC
marksD

After a few days, you need to store marks of a different subject ‘Chemistry‘ as well. What is the first thought that comes to your mind? How can you store the marks of this new subject?

Well, one approach could be to rename these variables as:

physicsA
physicsB
physicsC
physicsD

chemistryA
chemistryB
chemistryC
chemistryD

You just made sure that you can track the marks of each student in both the subjects. Suddenly, there is a new student ‘E‘ in the class and now you have 5 students.

You should start to get a little worried now as we need to keep a track of student ‘E‘ as well. You hesitate a little but soon enough come up with adding an additional variable for this new student. The total number of variables now look like:

physicsA
physicsB
physicsC
physicsD
physicsE

chemistryA
chemistryB
chemistryC
chemistryD
chemistryE

Everything works fine for some time, but then suddenly there is a new requirement of keeping a track of marks in Biology as well. What do you do now? Sure, you can add more variables, but one can imagine how hard it will get to keep a track of everything.

This is where you want to introduce yourself to the concept of arrays.

#### What is an Array?

An array is a linear data structure that is capable of holding values that are of a similar type. They can store numbers, strings, boolean values(true and false), characters and so on. But once you define the type of values that your array will store, all its elements must be of the same type. You cannot have an array that has some elements as string, and others as integers.

This is how a valid array looks like:

When we say int[ ] x = { 4, 8, 15, 16, 23, 42 }, we define an array of size 6 with the given elements. In computer programming, you begin the indexing from 0. To access these elements we can simply do:

x[0] = 4
x[1] = 8
x[2] = 15
x[3] = 16
x[4] = 23
x[5] = 42

What we just did is, we stored all the similar kind of data in just a single block. If you think about it, it makes sense logically as well because the data that has a similar characteristic should be together. So, in our above example of the classroom you know that marks in physics and marks in chemistry would have a similar type. Thus, we can declare some arrays like:

int[] physics = { }
int[] chemistry = { }
int[] biology = { }

Do you see how we are able to drastically reduce the number of variables required? In this scenario, if you want to add another subject just create an additional array and you can keep on writing your code.

#### Limitations

As good as an array may sound, you must keep in mind some limitations when using them:

1. You should always have memory available before declaring an array. This means that if you trying to declare an array for 20 elements, but you only have space in the memory for 19 elements, the declaration would fail. Your program would give an out of bounds error.
2. You cannot add additional elements between an already created array. Suppose you have an array with some elements as { 2, 4, 6, 8, 10 }. If you want to add an additional element between the number 2 and 4, this is not possible.

Both of these problems are however resolved by the Linked List data structure. You can read about them once you completely understand the working of arrays.

#### You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More