DEV Community

Cover image for ARRAYS- Let’s put some human touch to (this.concept).
aidelojep
aidelojep

Posted on

ARRAYS- Let’s put some human touch to (this.concept).

ARRAYS- Let’s put some human touch to (this.concept).

SERIES 1: BRIEF INTRODUCTION TO ARRAY CONCEPT.

Earlier on, whenever the term “Array” popped up, I used to think to myself, there goes another buzz word, Programming terminology. I struggled to relate this concept to real life scenarios because it felt totally strange and a bit abstract.
Later on, after some level of study and familiarization with the concept of Array, I became more aware of the subconscious application of array concepts in our daily lives. This reality made me look out for ways on how I could apply this new found knowledge to daily life. Simply put, arrays are just variables containing values of similar datatypes.

Variables can be referred to as a container for holding objects or values. So, since we agreed on referring to Arrays as containers, it is safe to assume that these containers can be used as a storage unit for stuffs.
Imagine using our refrigerator to store cool soft drinks of different varieties.
I’m sure a cold drink in a hot weather(tropical) region in Sub-saharan Africa would always be a delight.
Now back to our refrigerator, these drinks contain Coca-cola drinks, guinness drink, milk shakes and vodka at some different position or location in the refrigerator. The position or spot where each drink is located is what we refer to as “INDEX”. It helps us to determine where a particular array value lies in a wide variety of sets. Ofcourse, it becomes easier for me to locate a bottle of coca cola based on where it is positioned in the refrigerator set. Wow! Aren't Arrays just beautiful??!

refrigerator example

Also, we must note that in numbering the index of an array, we must start our counting from zero. This is borrowed from a well established mathematics concept of counting. The concept of the number index counting system can be related to Ricci calculus.

In programming, arrays can be symbolized with a set of open/close square brackets [ ]. This is because it serves as a container to hold items. Since, we already established that arrays are a set of variables for holding similar values, therefore, it makes total sense for these containers to hold these values. If you have something of value given to you, you would want to protect it and hold it tight from breaking or falling off. This is exactly what an array does with the values you give to them, they keep them safe and provide them to you whenever you want them to be rendered back.
Also, one very important aspect of arrays that we should also consider is that: “Arrays hold values of similar data types”.
A “data type” refers to the descriptive nature of how the values in a containers behave, it tells us more about their state-it answers questions like: Could these values you trying to store up in the arrays be numbers (integers) , alphabets (strings), or single letter/number/symbols (characters), true or false statements (boolean).
Data types help variable or container objects to create a safe haven i.e a welcoming ground that will be suitable for the arrival of the value that you intend to keep inside of them. For instance, you do not expect to keep toiletries in the kitchen or your shoes in your parlour. A housing apartment is designed in such a manner , such that every item has it’s designated position. This would help to reduce the level of confusion and make things easier to coordinate.

ARRAY LENGTH:
Arrays determine their length based on the number of items it contains. But the little trick to this is, an array length is different from number of items in an array. The array length is usually calculated by saying: Array length minus one. Remember we already established that an array numbers begins from zero, therefore the last item in an array would always be lessed than its length. This concept also helps us to avoid an error known as “ArrayIndexOutOfBound”. We would consider this concept later.

The array length can be calculated by:
arrayName.length which returns the array length.

PS: ‘for loop’ uses array.length to determine the length of the array.

ARRAY INDEX:
The index of an array simply refers to the “position number” of the elements in the array(counting from zero).
ARRAY DEFINITION:
Arrays are groups of variables (called elements/components) containing values that all have the same data types. ARRAYS are OBJECTS, so they are considered “reference types”. To refer to Array elements/values , we make reference to their index.
ZEROth ELEMENT:
This refers to the first element of an array. Please note that an array index must not be negative i.e (non-negative integer).
Please note that all array indexes must be an integer value. It is usually denoted with “int”.

DECLARING AN ARRAY:
Array objects occupy space. Arrays are created using the keyword “new”.
Therefore, to create an array object, you have to:
i) specify the type of array elements (data type).
ii) specify the number of array elements (array length).
The array creation expression below explains better:

int array firstName [ ] = new int [10 ];

where int = refers to the datatype
firstName= refers to the arrayName
new= is the keyword used to create new array
[10]= refers to the length of the array.

ARRAY VALUE:
Array values are denoted using array[counter] assuming counter is what is used in your array creation expression.
ARRAY INITIALIZER:
You can decide to create or declare an array through various ways. You can create an array and initialize it’s elements with ‘array initializer’- a comma separated list of expressions (called an initializer list) enclosed in curly braces {}.

NB: In this case, the array length will be determined by the number of elements in the initializer. E.g

int [] = {2,4,6,8,10,12,14};
The above expression is a 6-element array with index (0-5).

CONCLUSION
We have finally come to the end of our “Introduction to arrays” (series 1).
So, there you go! You should be proud of the progress you’ve made so far on array. We were able to cover areas like:
What is an array?
Array length.
Array initializer.
Array declaration.
How to create/declare an array.
Trust me, you’ve been absolutely amazing! Next episode of the array series, we would look at other aspects of an array and array list like

For now, cheers and goodluck!
This article on array is going to be in three series, namely:

Arrays series 1. (INTRODUCTION).

Array series 2.(INTERMEDIATE).

Arrays series 3. (ADVANCED).

Watch out for this space for more engaging contents on array.

Top comments (0)