
What is an Array?
An array is a linear data structure in which we store data and perform any operation, we can randomly access data in an array (With the help of its index values).
In other words, an array is a collection of similar types of elements (Homogeneous elements) that have contiguous memory locations i.e One after another.
Arrays store the related information in adjacent memory blocks.
If you need to access one or more than one element, the process of finding or performing operations on that element becomes very fast because your computer very well knows where the value is located in the memory.
Let’s Visualize arrays..!
To understand how the array works, Visualize your computer’s memory as a continuous block or grid. Each piece of block or grid contains information/data that is stored in it.
Why are Arrays ‘0’ indexed?
Counting arrays from the index value 0 simplifies the computation for the memory. Though it simplifies the computation. But it adds an extra step of an unnecessary subtraction of 1 i.e (n-1) for each access.
The array of length n can be indexed by the integers from 0 to n-1.
Most programming languages have been designed in this way only, so indexing from 0 is pretty much inherent to the language.
Defining an array:
As we already specified, the array contains homogeneous data so the above one is an integer array as it contains integer data.
Arrays can store numbers, strings, boolean values, characters, objects, etc. But once you define the type of values that your array will store, all its elements must be of that same type. You cannot “insert” different types of data in a single array.
Arrays cannot store heterogeneous data, Now what it means.
Let’s understand it through some visuals.
Creating an array :
Assign it to a variable (Name of an array).
Define the type of elements that it will store (integer, string, boolean).
Define its size (the maximum number of elements it will store).
Syntax : Data_type array_name [Array_size] ;
E.g: int myarray [8];
- Int => Datatype.
- myArray => Name of an array.
- [8] => Size of an array.
When you define the size of the array, all of that space in memory is “reserved”
Sample Code:
// An array of character char a1[] = {'t', 'u', 'f'}; // An array of integers int a2[] = {11, 22, 33, 44}; // We can access the array elements with the index values //E.g: a1[0] gives us 't'. //E.g: a2[1] gives us 22.
Finding an Element in an array
You have three options to find an element in an array:
- If we know where the element is located,We can use the index values to access it.
- If you don’t know where it’s located, you can use algorithms to optimize your search, such as Linear, Binary Search, etc.
Summary of the Arrays:
- Memory is allocated instantly: After the array is created the memory is allocated instantly and the array is empty until you assign the values.
- Elements are located in contiguous manner: So the elements can be accessed very efficiently (direct access, O(1) = constant time) using it’s index values.
- Arrays are powerful data structures: The type of elements and the size of the array are fixed and defined when you create it. Arrays store elements of the same type (homogeneous).
- Inserting elements: Reach to the end of the array and insert the element. The insertion at the end of the array. Insertion takes constant time O(1). We can also insert the elements in the middle or the start of the array but it’s a bit of a complex process.
- Removing elements: Reach to the index and remove the element. The removal operation takes constant time O(1).
Special thanks to Abhishek Yadav for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article