In this chapter
- what are the limitations of static arrays
- overcoming problems with static arrays by using dynamic arrays
- tradeoffs and when to use dynamic arrays
- what does it mean to build a dynamic array
- the best strategies to grow and shrink dynamic arrays
In these first few chapters, we have discovered how versatile arrays are and discussed some of their applications. But have you noticed in all the examples we have seen, the maximum number of elements we can store, and thus the size of the array, is determined in advance and can’t be changed later? This can work in many situations but, of course, not always—it would be naïve to think so.
There are many examples of real-world applications where we need to be flexible and resize a data structure to meet an increasing demand. When the ability to adjust their size is added to arrays, we get dynamic arrays. In this chapter, we look at examples where flexibility gives us an advantage and then discuss how to implement dynamic arrays.
The limitations of static arrays
Arrays are cool, right? Our little friend Mario sure thinks so: they are quite handy for storing items, and you can access these items quickly if you remember their position in the array (that is, their index).
Mario is so excited about learning how to use arrays that he can’t stop talking about it! He shares his passion for STEM and computers with his friend Kim from school, who is just as geeky.