ARRAYS- Let’s put some human touch to (this.concept).
By: Peter Lucky Aideloje
Tags: Array Methods, Array Functions, Arraystudentblog, Arrays, Arraylist

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 a 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 in various ways. You can create an array and initialize its 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 an 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 good luck!

This array article 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 content on an array.

Kindly reach me on my socials for any questions:

Peter on LinkedIn

Peter on Twitter

Peter on dev.to

Peter on github