Array
In computer science, an array is a data structure that stores a collection of elements, all of the same data type, in contiguous memory locations. This arrangement allows for efficient random access to any element by its numerical index.
Key characteristics
Contiguous memory: The elements of an array are stored next to one another in a single block of memory. This improves performance by making it faster for the computer to access related data, a property known as "cache locality".
Same data type: All elements within a single array must be of the same type, such as integers, strings, or other objects.
Fixed size: In many programming languages, the size of an array is fixed at the time it is created. It cannot be expanded or shrunk during a program's execution without creating a new, larger array and copying the data over.
Index-based access: Each element in an array is identified by a numerical index. In most modern programming languages (like C, Java, and Python), arrays use a zero-based index, meaning the first element is at index 0.
Types of arrays
One-dimensional (linear) arrays: The simplest type, which can be visualized as a single row or list of data. For example, quiz_scores storing a list of test results.
Multi-dimensional arrays: Arrays that contain other arrays. These are used to represent data structures like tables or matrices. A two-dimensional array, for instance, has rows and columns, with elements accessed using two indices, such as array[row][column].
Jagged arrays: A special type of multi-dimensional array where the lengths of the inner arrays can be different. This is useful for storing non-uniform tabular data.