Introduction to Arrays

Arrays are everywhere! If you are exploring a new programming language, you will notice that arrays are the first and most important data structure to learn. Arrays are important as they hold multiple values in a single variable.


Why use Arrays?

Imagine you are dealing with hundreds or even thousands of records, and you want to loop through these records to get a specific element. An array makes the job super easy as you don’t have to define hundred or thousands of variables, just one! 

Some of the reasons why you must array in your code are below:

  • Less Code: You don’t need to define multiple variables.
  • Easy to traverse: You can quickly go through the array’s elements by using loops.  
  • Sorting: Can be sorted alphabetically or numerically.

Your code with and without arrays

Let’s say you are a boss and you have ten employees. You want to create a program to manage your employee’s affairs. Without arrays, you will need to define variables for each employee:

Employee0; 
Employee1; 
Employee2; 
Employee3; 
Employee4; 
Employee5; 
Employee6; 
Employee7; 
Employee8; 
Employee9;

Your code with using arrays

Employee[10];

How arrays are stored in memory?

Arrays are stored in the memory in contiguous locations in the RAM ( random access memory). This means all array elements are stored within a defined memory range. 

Note
Arrays are always of the same type; A string array will always contain string variables