Chapter 8

Understanding Arrays and Collections

What you will learn in this chapter:

  • Arrays
  • Array indexes and elements
  • How to set the size of an array
  • Some of the array class methods commonly used
  • Multidimensional arrays
  • Array lists
  • Collections
  • Jagged arrays

wrox.com code downloads for this chapter

You can find the wrox.com code downloads for this chapter at www.wrox.com/remtitle.cgi?isbn=9781118336922 on the Download Code tab. The code in the Chapter08 folder is individually named according to the names throughout the chapter.

In this chapter you learn about arrays, array lists, and collections. These data structures are useful to solve many types of programming problems. When you finish this chapter, you'll have an appreciation of how arrays can make certain programming problems much easier to solve.

What Is an Array?

An array is a group of identical data types that share a common name. The syntax to define an array is as follows:

typeSpecifier[] arrayName = new typeSpecifier[numberOfElements];

where:

  • typeSpecifier is the data type you want to use.
  • arrayName is the name you want to use for the array.
  • numberOfElements is the number of array elements you want.

Each of these parts is explained in a moment using the following example.

Suppose you have an address book with 100 names in it. Because you want to manipulate those names in some way in your program, each unique name must be stored in a unique variable. Because names are textual data, you could use something like this: ...

Get Beginning Object-Oriented Programming with C# 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.