AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Introducing Data Structures
  • Array
  • ArrayList
  • 2D Array
  • HashMap

Was this helpful?

  1. Data Structures

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

PreviousData StructuresNextIntroduction to Arrays

Last updated 5 years ago

Was this helpful?