In this article, we will take a look at the advantages and disadvantages of one of the most basic data structures in programming – Arrays. But before that, let us have a brief understanding of what arrays are.
An array is one of the most popular data structures in various programming languages like C, C++, Java, etc. It is used to store data in contiguous memory locations. Whenever we deal with an ordered set of data, arrays are the first and foremost choice to store this data. It can be used to store data of different data types like integer, float, char, double, etc, but a single array contains data of only a single data type. The values stored in arrays are identified by their indexes which generally starts from zero, i.e, the first element of an array will have index zero, the second element will have index one, and so on. These arrays are of great use while executing lengthy programs, but they have certain advantages and disadvantages too.
In the above representation, note that the next location is incremented by 4 and not by 1. This is because an integer takes 4 bytes of memory, therefore the memory location gets increased by 4. If you take any other data type, the memory address will vary accordingly.
Advantages of Array
- Arrays help in code optimization. We can store a large number of values in a single array by writing a small piece of code rather than declaring each variable separately.
- Arrays are easy to use as many algorithms like searching and sorting techniques, finding maximum and minimum values, reversing can be easily implemented using arrays.
- The time complexity to access any element of an array is O(1), i.e, it takes a constant amount of time to access an element.
- Arrays use indexes to identify their elements. These indexes starting from ‘zero’ and ending at ‘length of array – 1’ can be used to access all elements of an array.
- Along with simple arrays, we also have 2- dimensional arrays, which are used to store elements of a matrix of any dimensions.
- Since arrays store elements in contiguous memory locations, no extra memory is allocated outside this contiguous block, which prevents wastage of memory.
- Being one of the most basic data structures, arrays can be used to implement other data structures like linked lists, stacks, queues, graphs, trees, etc.
- Arrays can be used to implement many CPU Scheduling techniques.
Disadvantages of Array
- The size of an array is fixed. Once the memory is allocated to an array, it cannot be increased or decreased. This prevents us from storing extra data in case we want to. These arrays of fixed size are called static arrays.
- Allocating less memory than the required to an array leads to loss of data.
- A single array cannot store values of different data types, i.e, an array is homogenous in nature.
- The deletion and insertion operations in arrays are very difficult to implement as they store data in contiguous memory locations. To overcome this problem, linked lists are implemented which provide random access of elements.
- In C language, the compiler does not perform index bound checking on arrays. So, if we try to access an element using an index that is outside the range of indexes of an array, the compiler shows a run time error, rather than an index out of bounds error.
Despite many of its disadvantages, arrays are largely used in big projects because of their vast number of advantages. So, this is all about the advantages and disadvantages of arrays.