Hello Everyone,
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.
Now if take an example of implementation of data structure Stack using array there are some obvious flaw.
Let’s take the POP operation of the stack. The algorithm would go something like this.
- Check for the stack underflow
- Decrement the top by 1
So there what we are doing is that the pointer to the topmost element is decrement meaning we are just bounding our view actually that element stays there talking up of the memory space if you have any primitive datatype then it might be ok but the object of an array would take a lot of memory.
Examples –
// A character array in C/C++/Java char arr1[] = {‘g’, ‘e’, ‘e’, ‘k’, ‘s’};
// An Integer array in C/C++/Java int arr2[] = {10, 20, 30, 40, 50};
// Item at i’th index in array is typically accessed
// as “arr[i]”. For example arr1[0] gives us ‘g’
// and arr2[3] gives us 40.
Usually, an array of characters is called a ‘string’, whereas an array of ints or floats is called simply an array.
Applications on Array
- Array stores data elements of the same data type.
- Arrays can be used for CPU scheduling.
- Used to Implement other data structures like Stacks, Queues, Heaps, Hash tables, etc.