Introduction to Array Datastructure
Ram understands very well that in order to tackle any programming challenge, he must learn about data structures and methods. Ram told himself, "I'll start with each datastructure and fully understand it before moving on to learning different algorithms." He then started studying by opening his laptop.
While Ram was hovering through all the datastructures, he encountered Array would be nice to start with. He mumbled few lines about Array, until he heard a loud voice from kitchen, “Raaam, get 10 Eggs from market, take the egg carton from the fridge and leave immediately.” Yes, that was his mother asking him to get eggs. Ram knowing what would happen if he didn’t start, left immediately with the egg carton which can hold up to 10eggs.
On his way to the store, Ram is clutching the carton and smiling as he recalls a memory. Last time when he forgot to bring the carton to buy eggs, Ram tried to hold all eggs in his pockets and few in hands. Eventually when he reached home, all his pockets were wet as a couple of eggs were broken in his jeans and got nice yell from his mother.
On his way back, Ram ran into two of his buddies, Abdul and Robert, who had also purchased eggs. Ram first saw Abdul on his journey, who, like him, carries a carton.
Ram: Abdul, did you buy ten eggs like me?
Abdul: No Ram, only two eggs.
Ram: Then why did you bring that carton with you, which you can carry with your hands and is a waste of space?
Abdul: Yes, Ram, but mom instructed to bring eggs with the leftovers from the vegetables. I only had enough money to buy two eggs after I bought everything.
Recommended by LinkedIn
Ram then understood and waved goodbye. He ran across Robert on his way home after Abdul. Ram told Abdul's complete narrative and spotted Robert holding two eggs in his left hand and a carton in his right.
Ram: Hello, Robert. Why are you transporting eggs by hand when a carton is available?
Ram: The carton is full, Robert. As the price of eggs fell, the shopkeeper gave two extra eggs. Goodbye, Ram.
Ram acknowledged Robert's understanding and waved before following him home. Now Ram sat down after the market to unwind a little. He now pulled out his book once more to study arrays. Ram learned that an array holds data of related datatypes in a sequential manner. When Ram read that an array is static in nature and that the size must be fixed when declaring it, he immediately thought, "Yes, it's just like that carton box of all the eggs (similar datatype)," but what really excited him was the statement that if the user declared a large size but used a small number of array blocks, the remaining space would be wasted.
Similarly, if a user declares less space than necessary but later realizes that they require more space than they originally thought. Even in that case, it is a problem with Arrays. So, programmer must be aware of the size before while declaring an array, just like Ram carried carton of size 10, when he know he has to carry 10 eggs. He was surprised to discover that he had a real time experience of what he’s learning with two of his buddies when returning home. This improved Ram's comprehension of the Array Datastructure and made him more enthusiastic learning Arrays.
Array is a fundamental datastructure, Read more about arrays below.
Wishing you a happy learning... :)