Arrays, Our First Data Structure!
We've arrived at our first CS 70 data structure: the simple, often underappreciated array.
You probably already have experience with arrays from working with them in Java.
Yes, I totally know Java. So C++ will be just the same?
There are many similarities, but also significant differences in what you need to write to express similar ideas. Often prior Java knowledge will lead you astray, so be on the look out for differences.
I've also used Python arrays!
Actually, I think you're thinking of Python's “list” data structure. It does have many array-like properties, but it is like Java's
ArrayList
class, not basic Java arrays.
Properties of Arrays
Fundamental Properties of (Primitive) Arrays
An array is contiguous, ordered, homogeneous, fixed-size storage. Let's break that down:
- Contiguous: The items in the array are all together in memory with no gaps.
- Ordered: The elements in an array have a clear order to them. We know which one is "first" and which one is "forty-second."
- Homogenous: The elements of an array all have to be the same type.
- Fixed size: Once we create an array, we can't change the number of things it can hold.
Sure, okay. Do I need to remember these properties?
Actually yes, we'll put it on a test at some point, because Learning Goal 2A is “List and define the four fundamental properties of C++'s (primitive) arrays”, these exact properties.
Why Are Arrays Underappreciated?
There will be very few times when a fixed-size array will be the right data structure for a big program. On Homework 4, we'll learn about std::vector
, which can do everything an array can do without the fixed-size restriction.
So why take the time to learn about arrays?
Arrays are the fundamental building block of lots of other, fancier data structures—including
std::vector
.Understanding arrays will help us understand what's going on in memory when we use (and build) other data structures.
(When logged in, completion status appears here.)