TutorChase logo
Login
AQA A-Level Computer Science

2.1.2 Arrays: single- and multi-dimensional

Arrays are fundamental data structures in computer science, allowing efficient storage, access, and manipulation of sequential or tabular data in programming contexts.

What is an array?

An array is a structured way to store a collection of values of the same data type under one name. Each individual value in the array is called an element, and each element can be accessed using its index position. Indexing typically begins at 0 in most programming languages.

Arrays provide a convenient and efficient method for working with data sets where items are logically related and accessed using a predictable indexing pattern. They are static in size, meaning their length must be known or defined when the array is created.

Arrays are essential for:

  • Processing collections of data efficiently.

  • Representing linear or tabular information like lists or matrices.

  • Reducing the need for multiple variables and improving code organisation.

One-dimensional arrays (1D arrays)

Definition and concept

Take your grades to the next level!

UPGRADING TO PREMIUM UNLOCKS
AI Tutor
AI-powered study assistant
instant feedback and guidance
Predicted Papers
Examiner-style predicted papers
based on recent exam trends
Practice Questions
All exam practice questions
by topic for each subject
Study Notes
All detailed revision notes
written by expert teachers
Cheat Sheets
Quick revision summaries
perfect for last-minute review
Past Papers
Complete collection
of practice and past exam papers
Email
Password
Confirm Password
Already have an account?

Practice Questions

FAQ

Shallow copying an array creates a new array structure but does not duplicate the individual elements; instead, it copies the references to those elements. This means that if the array contains other arrays (as in a 2D array), both the original and the copied array will refer to the same inner arrays. Modifying an element in the inner array of the copied version will also affect the original array. Deep copying, on the other hand, involves duplicating the entire structure, including all nested arrays. This produces an entirely independent copy, where changes made to one array do not affect the other. This distinction is especially important in multidimensional arrays used in algorithms that rely on temporary data structures, such as backtracking or simulations. If a shallow copy is used where a deep copy is needed, the program might behave unexpectedly, leading to bugs that are difficult to trace. Most languages require specific functions or modules to perform deep copying.

In programming languages that only support fixed-size arrays (such as Java or C), resizing an array dynamically requires manual implementation. This involves creating a new array with a larger size, copying all existing elements into the new array, and discarding the old one. The process usually follows these steps: first, allocate a new array with the desired size (often double the original size for efficiency); next, iterate over the original array and copy each element into the corresponding index in the new array; finally, update the reference to point to the new array. This method allows for simulated dynamic resizing but comes at a cost of increased memory usage and time during the copying process. This technique underpins many high-level dynamic data structures like Python lists or Java ArrayLists, which handle this process automatically behind the scenes. However, understanding how it works manually is crucial for working in low-level contexts and optimising performance.

Jagged arrays are arrays of arrays where each "row" can have a different length, unlike regular two-dimensional arrays that form a uniform matrix with equal-sized rows and columns. In jagged arrays, each element in the primary array is itself an array, and those inner arrays can vary in size. For example, one row might contain three elements, while another contains five. This flexibility is useful when dealing with data that naturally varies in length, such as storing student test scores when students attempt different numbers of questions, or representing a triangle of values in a mathematical context. Unlike regular 2D arrays that occupy a contiguous block of memory, jagged arrays may result in less memory usage when the structure is sparse or uneven. Access is still performed using two indices (e.g., array[2][1]), but it's important to check the length of each sub-array to avoid index-out-of-range errors. Jagged arrays offer more control over memory and data layout.

A two-dimensional array is ideal for simulating a spreadsheet, where each element corresponds to a cell identified by its row and column index. Each cell can store a value such as an integer, float, or string, depending on the application. For example, spreadsheet[1][2] might represent cell C2. To update a value, simply assign a new value to the cell index. For calculations, loops can iterate over rows or columns to perform functions like sums, averages, or counts. For instance, to compute the total of a column, iterate over the same column index across all rows. You can also perform conditional formatting by checking if cell values meet certain criteria and then modifying their contents accordingly. Implementing formulas requires a mapping between formula strings and operations, often by using dictionaries or switch statements. While this simulation lacks GUI elements, it lays the foundation for understanding how spreadsheet software performs computations under the hood.

Multidimensional arrays are typically stored in memory either using row-major order or column-major order. In row-major order (used by C, Python, and most modern languages), all elements of the first row are stored contiguously, followed by all elements of the second row, and so on. In column-major order (used by languages like Fortran and MATLAB), all elements of the first column are stored first, then the second column, and so on. This affects performance because accessing elements in the same memory block (i.e., sequentially in row-major or column-major order) benefits from cache locality, speeding up access time. If elements are accessed in an order that matches the memory layout, the processor can pre-fetch nearby elements efficiently. However, accessing elements in the opposite order (e.g., column-by-column in row-major storage) may cause cache misses and degrade performance. Understanding the underlying memory layout helps in writing optimised code for applications like image processing or numerical simulations where large arrays are used.

Hire a tutor

Please fill out the form and we'll find a tutor for you.

1/2
Your details
Alternatively contact us via
WhatsApp, Phone Call, or Email