113: Data Types: Arrays.

Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Kategorier:

You’ll often need multiple variables of the same type and while you can sometimes just create separate variables with their own names, what if you don’t know ahead of time how many will be needed? Even if you do know how many variables are needed, sometimes there are just too many to give each one it’s own name. If you need a hundred bools, do you really want to work with them individually as doorOpen01, doorOpen02, etc.? No. Because this makes your code brittle and completely tied to that specific scenario. How many buildings do you know that have exactly 100 doors? You want your code to be able to work with multiple doors and know if they’re open or closed. But you don’t want to tie it to any specific number. By making your code independent, it can then work on buildings with no doors at all up to giant palaces with thousands of doors. Arrays let you work with multiple data types. You can still access each element individually. You just don’t need to give each one its own name that the compiler knows about and must track. You can also declare multidimensional arrays that have 2 or more indexes. How many items are in the array or can be in the array is very important. But it’s not always part of the type. There are some programming languages like C++ where this is part of the type. So an array of 3 ints is a completely different data type than an array of 4 ints. But even C++ can allow some unwanted flexibility here. Listen to the full episode or you can also read the full transcript below. Transcript Even if you do know how many variables are needed, sometimes there are just too many to give each one it’s own name. If you need a hundred bools, do you really want to work with them individually as doorOpen01, doorOpen02, etc.? No. Because this makes your code brittle and completely tied to that specific scenario. How many buildings do you know that have exactly 100 doors? You want your code to be able to work with multiple doors and know if they’re open or closed. But you don’t want to tie it to any specific number. By making your code independent, it can then work on buildings with no doors at all up to giant palaces with thousands of doors. Arrays let you work with multiple data types. You can still access each element individually. You just don’t need to give each one its own name that the compiler knows about and must track. There’s a lot you can do with arrays once you understand how to use them. If you need to read some data from a file into memory, then an array can be very useful. Episode 39 already discusses arrays as a type of collection. Refer to that episode for more information. This episode will fill in some gaps not covered by episode 39. Listening to this episode alone won’t be enough for you to understand arrays. But I also don’t want to repeat concepts that are already explained very well in other episodes. This episode focuses on the array as a data type. When you declare an array, you need to specify what type the array will hold. This could be an array of bools. Or an array of long integers. Or an array of chars. An array of chars is special and will be the topic of the next episode. In some languages, this is all you need to have a string. You can also declare multidimensional arrays that have 2 or more indexes. How many items are in the array or can be in the array is very important. But it’s not always part of the type. There are some programming languages like C++ where this is part of the type. So an array of 3 ints is a completely different data type than an array of 4 ints. But even C++ can allow some unwanted flexibility here. One way to make sure that you always get a type with a specific size is to create a class to wrap up the array. The C++ language has the ability to create templates that take a value as one of the template arguments. You could create your own individual class type based on a template th

Visit the podcast's native language site