Chapter 9. collections: Get Organized

image

Ever wanted something more flexible than an array?

Kotlin comes with a bunch of useful collections that give you more flexibility and greater control over how you store and manage groups of objects . Want to keep a resizeable list that you can keep adding to? Want to sort, shuffle or reverse its contents? Want to find something by name? Or do you want something that will automatically weed out duplicates without you lifting a finger? If you want any of these things, or more, keep reading. It’s all here...: Get Organized

Arrays can be useful...

So far, each time we’ve wanted to hold references to a bunch of objects in one place, we’ve used an array. Arrays are quick to create, and have many useful functions. Here are some of the things you can do with an array (depending on the type of its items):

image

...but there are things an array can’t handle

Even though an array lets you perform many useful actions, there are two important areas in which arrays fall short.

You can’t change an array’s size

When you create an array, the compiler infers its size from the number of items it’s initialized with. Its size is then fixed forever. The array won’t grow if you want to add a new item to it, and it won’t shrink if you want to remove an item.

Get Head First Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.