What is a Data Structure?
Let’s start with something familiar: the array.
You have likely met arrays in your first programming course as a way to hold a list of values. The array is the canonical example of a data structure where we organize a collection of related data so we can get at any piece of it quickly.
An array lets us treat a whole collection as a single entity, and still reach any individual element directly through its index.
Here is the syntax in Java:
int[] numbers = {10, 25, 7, 42, 18}; // Array declaration and initialization
System.out.println(numbers[0]); // Access first element: 10
System.out.println(numbers.length); // Get array size: 5
numbers[2] = 99; // Modify element at index 2
System.out.println(numbers[2]); // Now prints: 99
The array numbers is a data structure because it organizes data (a collection of integers) in a way that allows us to perform certain operations efficiently (like accessing any element by index). We will use a three-part definition of data structures to understand this better:
- Organization: how the data is laid out. Which elements are grouped together, what order they are in, and how they relate to each other.
- Operations: what we are allowed to do with it. These are the reads and writes the rest of the program can ask for.
- Trade-offs: what we get from the way the data is organized, and what it costs us. Some operations get faster. Others get slower, or harder to express.