6. Arrays#
When you need to store many values of the same type, arrays are the answer. This chapter covers one-dimensional and multi-dimensional arrays, including how to declare, initialize, and traverse them efficiently.
Why this chapter matters
Arrays are the foundation of all structured data storage:
hold a fixed number of values under one name
access any element in constant time using an index
pass collections of values to and from methods
model grids, matrices, and tables with 2D arrays
Learning goals
By the end of this chapter, you should be able to:
declare, initialize, and access 1D arrays
traverse an array with
forandforeachloopsdeclare and work with 2D rectangular arrays
pass arrays to methods and return them as results