• 4 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 6/15/21

Recognize the array data type

One thing I have discovered working in the software industry is that as soon as you give the customer a shiny new object, they want another. What do we do with so many shiny objects? Well, we can use the array data type to store them all together! In other words, our variable can have a type which allows it to store groups of data. You can think of them a bit like buckets in an egg carton:

Container
Container

The array container shown here stores data in individual buckets.  Not surprisingly, the array itself is an object that stores your objects.  It might ultimately contain 12 egg objects, but at the moment the array is empty. That's allowed, though!

An array data type must be sized when it is created, so in this case, you can store no more than a dozen eggs. Even though you have to specify the size, it can be as big as you want. We've talked about eggs and auditoriums, but arrays can be created for many different purposes.  Maybe you want to store student, automobile, or even planet objects in an array!

Array buckets can be accessed in constant time (very fast) so even if there were two-million eggs in your container, the time to fetch an egg is the same as if there were only a few eggs. Neat, huh?

Accessing array buckets

Array bucket numbering is zero-based, meaning that counting starts at 0, not 1.  Therefore a dozen egg buckets would be number 0-11.  These array buckets are accessed using the following notation: 

  • eggs[0]

  • eggs[3]

Below are the names of the arrays (eggs) followed by location. This example addresses the first bucket and the fourth bucket respectively. Let's add five eggs to the array. To do so, add what's located at each spot:

  • eggs[0] = Grade A large egg

  •  eggs[1] = Grade A large egg

  •  eggs[2] = Grade A large egg

  •  eggs[3] = Grade A large egg

  •  eggs[4] = Grade A large egg

Six Eggs
Five eggs

In this case, all the items are called the same things!

Try it out for yourself!

Let's play an array game! Here is one which we've named "people."

"Scott"

"Hayley"

"Tom"

"Susan"

"Lori"

"Lars"

"Bob"

"Eric"

"Jay"

"Clay"

Using this array, try your hand at answering the following questions:

  1. What is the length of this array?  (how many buckets)

  2. Write the correct syntax to access "Lori." 

  3. What is stored in people[0]?

Summary

  • An array is a container for other objects.

  • The array contains a fixed number of buckets that you specify when it is created.

  • Array buckets are accessed using zero-based counting.

  • Array processing is very fast.

Example of certificate of achievement
Example of certificate of achievement