Array is an important data structure. We can define it by saying that it is a collection of like objects. The objects are called elements which are present at contiguous memory locations. Every element of array is accessed by using its unique index. Indices of arrays always start from zero.
Merits of Array:
1: Ease of declaration:
Arrays provide us a handy way to declare variables. We don’t have to declare as many variables as values are being stored. We just declare an array and give it a size. For example, if you need to declare 10 variables of integer type then you can use an array. You will not have to declare ten different variables. You just declare an array of size 10.
Above C++ statement will allocate ten memory locations for array variable.
2: Ease of access:
The main advantage of array is that we can access a random element. In other data structures like linked list, we have to traverse the list to find a specific element. But in array, we just give the specific index of element and use it. It saves a lot of time and memory.
3: Contiguous memory locations:
The elements of array are stored at consecutive memory locations. For example, if we have an array of ten elements then these ten elements will be stored consecutively in the memory. These consecutive locations help to access an element quickly.
4: Implementing data structures:
Other data structures like stacks, queues, trees etc are widely used. Arrays provide us such functionality that we can use these data structures easily.
5: Matrices Re presentation:
Arrays are not just one-dimensional arrays. 2-D (two dimensional) arrays are also present which help us to represent matrices.
Demerits of Array:
1: Wastage of memory:
The biggest drawback of array is that it is fixed length. Once you have declared it, you cannot change its size at run-time. For example, if you are asked to design a program for data entry and you don’t know the exact number of records to be entered. Then you will probably declare an array of maximum size you can imagine according to your problem. Now, if user enters records less than size of array then empty memory spaces would be wasted. So, this can cause wastage of memory.
2: Similar data type:
You can store only those elements in array which are similar in data type. Actually when you declare an array then you also tell compiler its data type. In linked list, you can store data of more than one type in a node. So, similar data type can be considered as a drawback of array.
3: Consecutive memory locations:
Though consecutive memory locations are useful for fast accessing of elements of an array. But this consecutiveness also makes hard the deletion and insertion of elements. For example, if you delete an element from array then you will have to traverse the array to adjust the places of remaining elements. Traversing of array is a difficult and time-consuming job. This thing also occurs in case of inserting an element in array.
Kamal Choudhary is a tech geek who writes about C++ programming tutorials on C++ Beginner. He is a student of computer science in University of Gujrat, pakistan. He loves to write about computer programming. You can find his full bio here. Follow him on twitter @ikamalchoudhary