O'Reilly logo

Beginning Programming ALL-IN-ONE DESK REFERENCE FOR DUMMIES® by Wallace Wang

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter III.3. Collections and Dictionaries

An array can be handy when you need to store the same type of information, such as a group of integers. However, if you need to store different information, such as both integers and strings, and you aren't sure how many items you need to store, you probably can't use an array. Instead, you can use a collection or a dictionary.

A collection acts like a resizable array that can hold different data types at the same time while identifying each chunk of data with a number. A dictionary acts like a collection that identifies each chunk of data with a unique key.

The purpose of both collections and dictionaries is to make it easier to store different types of data and retrieve them again with the size and single data type restrictions of an array.

Using a Collection

A collection acts like a super array that can grow and expand without requiring any special commands. In addition, a collection can store different data types (such as integers or strings) or even other data structures, such as an array.

Note

Not all programming languages offer the collection data structure:

  • In some programming languages (like Python and Smalltalk), collections are a built-in feature of the language.

  • In other languages (like C or Pascal), you have to use more primitive data structures (like arrays) to mimic the features of a collection.

  • In many newer languages (like C# and Visual Basic.NET), someone else has already created a collection out of more primitive data structures, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required