One of the most common features of procedural programming languages is the concept of an array. Arrays seem like simple things but there are many questions that must be answered when adding them to a language, such as:-
fixed-size or variable-size?
is the size part of the type?
what do multidimensional arrays look like?
does the empty array have meaning?
What are Arrays?
An array is a data structure for storing more than one data item that has a similar data type. The items of an array are allocated at adjacent memory locations. These memory locations are called elements of that array. The total number of elements in an array is called length. The details of an array are accessed about its position. This reference is called index or subscript.
Concept of Array:-
The above diagram illustrates that:
- An array is a container of elements.
- Elements have a specific value and data type.
- Each element also has its own index, which is used to access the element.
Types of Array:
Arrays can be single or multidimensional. The number of subscript or index determines the dimensions of the array. An array of one dimension is known as a one-dimensional array or 1-D array, while an array of more than one dimensions is known as a multi-dimensional array that can be 2-D or 3-D.
Why do we need Arrays?
- Arrays are best for storing multiple values in a single variable.
- Arrays are better at processing many values easily and quickly.
- Sorting and searching the values is easier in arrays.
Overview of Various Array Operations:-
In traversing operation of an array, each element of an array is accessed exactly for once for processing. This is also called visiting of an array.
Insert operation is to insert one or more data elements into an array. Based on the requirement, new element can be added at the beginning, end or any given index of array.
Deletion refers to removing an existing element from the array and re-organizing all elements of an array.
You can perform a search for array element based on its value or its index.
Update operation refers to updating an existing element from the array at a given index.
Advantages of using Arrays:-
- Arrays allow random access to elements. This makes accessing elements by position faster.
- Arrays have better cache locality that can make a pretty big difference in performance.
- Arrays represent multiple data items of the same type using a single name.
Disadvantages of using Arrays:-
You can’t change the size i.e. once you have declared the array you can’t change its size because of static memory allocated to it. Here Insertion and deletion are difficult as the elements are stored in consecutive memory locations and the shifting operation is costly too!