What Are Data Structures?
Data is the name for the symbols or quantities on which a program operates. There are many types of data and many ways to use data in programs. For example, if you are interested in weather patterns, you would want to collect temperature readings every day for several years. Or if you are working on a book of knock-knock jokes, you'd need to find as many knock-knock jokes as possible.
However, data isn't of much use on its own. Without programs to interpret them, data would just be a pile of symbols, numbers, and other characters. Programmers write code that takes in and processes data to make sense of the collected information. Because data is intended for use in programs and analysis, it needs to be organized. Hearkening back to the temperature collecting example, the data wouldn't be very useful if you wrote down a bunch of scattered numbers on one page and wrote the dates and times in a different order spread across other pages. Disorganized data is difficult for both computers and humans to understand.
Introducing Data Structures
Data structures are particular ways of organizing data in programs. Just as there are many different types of data, there are many different types of data structures. We will cover a few of the most important data structures in this course: arrays, ArrayList, 2D arrays, and HashMaps.
Array
For example, imagine that you need to buy exactly 10 items at the grocery store. Rather than trying to remember every item to purchase, you find a piece of paper that is just big enough to write down a list of these 10 items and bring that list with you to the store. This type of data structure is called a list or array.
ArrayList
But what if your friend calls you on the way to the store and asks you to purchase a few items for her? Recall that the array has a fixed size, since the piece of paper you used to write it down on was only big enough for 10 items. In this case, you would want to use a list that would let you add and remove items as you go. This kind of flexible list is called an ArrayList.
2D Array
Say that after going grocery shopping, you and your friend decide to play a game of checkers. A checkerboard is a grid of rows and columns, with each piece stored on its own location in the grid. A grid is a type of data structure commonly referred to as a 2D array, since it has two dimensions (rows and columns).
HashMap
Or perhaps you are going on vacation to an exciting place and you want to send postcards to your friends while you are away. You could create an address book to take with you by writing the name of each friend in one column and the corresponding mailing address in the next column. This data is different from the list of grocery items because each entry actually consists of two pieces of information: a key (the friend's name) and a value (the friend's address). As such, you would use a different data structure called a map to store this type of data (so called because it "maps" each name to an address).
In summary, the data types are:
Type | Simple Definition | Example |
---|---|---|
array | List of fixed size | 10-item grocery list |
ArrayList | List of changing size | Flexible grocery list to add or remove items |
2D array | Table of grid | Checkerboard |
HashMap | Association, key to value | Address book to store friend's addresses |