You might be familiar with maps like street maps and treasure maps, but that’s not what we’re talking about here. In Kotlin, a table like this is called a map. Hammer Wrench Wrench Slotted Screwdriver Phillips Screwdriver Tool to Use Nail Hex Nut Hex Bolt Slotted Screw Phillips Screw Hardware Now that we’ve successfully made a single association, we can do the same thing for the rest of the tools… and then put them all together into a map! Map Fundamentals val association = Pair("Nail", "Hammer")Įxplicitly specifying the type of the association variable. In our case, let’s associate two String objects - one for a nail, and one for a hammer. You can call its constructor with any two objects, regardless of their types. The constructor of this class has two parameters. One simple way to associate two values is to use a class called Pair. In this chapter, we’ll build out Kotlin’s equivalent of a table like this, but before we do, let’s start by creating a single association. Tables like this show that there are associations between things - a nail is associated with a hammer, a hex nut is associated with a wrench, and so on. (Easy stuff!) Hammer Wrench Wrench Slotted Screwdriver Phillips Screwdriver Tool to Use Nail Hex Nut Hex Bolt Slotted Screw Phillips Screw Hardware Which tool should be used with a slotted screw? A Slotted Screwdriver! ![]() Just scan down the left-hand column for the hardware you need to work with, and then scan across to see the right tool to use.” How to scan a table. “Now, with this table, you can easily look up what tool you need. ![]() A two-column table of tools - hardware in the left column, tools in the right column. ![]() In order to help Jim pick out the right tool, he sketched out a table of the different hardware and tools in his toolbox. “When you’ve got a nail, you need to use a hammer, not a screwdriver.” Andrews taught his young son Jim, who was just starting to learn how to become a handyman like his old man. “You gotta use the right tool for the job.” That’s what Mr. In this chapter, we’re going to look at one more kind of collection - a map. In the last chapter, we saw how collections, such as lists and sets, can be used to do things that couldn’t be done easily with separate, individual variables.
0 Comments
Leave a Reply. |