15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3: Collections<br />

and Dictionaries<br />

In This Chapter<br />

Storing data in a collection<br />

Organizing data in a dictionary<br />

Understanding hash tables and collisions<br />

An array can be handy when you need to store the same type of in<strong>for</strong>mation,<br />

such as a group of integers. However, if you need to store different<br />

in<strong>for</strong>mation, such as both integers and strings, and you aren’t sure how<br />

many items you need to store, you probably can’t use an array. Instead, you<br />

can use a collection or a dictionary.<br />

A collection acts like a resizable array that can hold different data types<br />

at the same time while identifying each chunk of data with a number. A<br />

dictionary acts like a collection that identifies each chunk of data with a<br />

unique key.<br />

The purpose of both collections and dictionaries is to make it easier to store<br />

different types of data and retrieve them again with the size and single data<br />

type restrictions of an array.<br />

Using a Collection<br />

A collection acts like a super array that can grow and expand without<br />

requiring any special commands. In addition, a collection can store different<br />

data types (such as integers or strings) or even other data structures, such<br />

as an array.<br />

Not all <strong>programming</strong> languages offer the collection data structure:<br />

✦ In some <strong>programming</strong> languages (like Python and Smalltalk), collections<br />

are a built-in feature of the language.<br />

✦ In other languages (like C or Pascal), you have to use more primitive<br />

data structures (like arrays) to mimic the features of a collection.<br />

✦ In many newer languages (like C# and Visual Basic.NET), someone<br />

else has already created a collection out of more primitive data structures,<br />

so you can use them without knowing how they were created.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!