Course contentsShow
← Computer Science
Lesson 234 of 2,8726. Arrays and Basic CollectionsFree lesson

Array Indexing and Access

Using zero-based indexing to access and modify individual array elements by their position.

Array Indexing and Access

You'll learn how to reach into an array and grab or change a specific element using its position number.

Now that you know what arrays are and how to create them, you need a way to work with individual elements inside. This is where indexing comes in—think of it like apartment numbers in a building. Each element has a numbered "address" that lets you access it directly.

Zero-Based Indexing

Arrays use zero-based indexing, meaning the first element is at position 0, the second at position 1, and so on. If you have an array with 5 elements, the valid positions are 0, 1, 2, 3, and 4.

Why start at zero? It's a convention from how computers calculate memory addresses. Think of the index as "how many steps from the start"—the first element is zero steps away.

Accessing Elements

To read an element, you write the array name followed by square brackets containing the index:

scores[0]  // accesses the first element
scores[2]  // accesses the third element

Modifying Elements

You can also change an element by assigning a new value to that position:

scores[0] = 95   // changes the first element to 95
scores[3] = 88   // changes the fourth element to 88

Why This Matters

Indexing gives you random access—you can jump directly to any element in constant time, without looping through the entire array. This makes arrays incredibly efficient for retrieving or updating data when you know the position.

Key Takeaway: Arrays use zero-based indexing to access elements by position—array[0] gets the first element, array[1] the second, and you can both read and modify elements using this bracket notation.