Single- and Multi-Dimensional Arrays

Single- and Multi-Dimensional Arrays

Single-Dimensional Arrays

  • A single-dimensional array is the simplest form of data structure that can store a fixed-size sequential collection of elements of the same type.
  • Also known as one-dimensional array, it represents a list of variables sharing the same datatype.
  • Each element in this array is associated with a unique index that allows direct access to the element.
  • In programming, index usually starts with zero, meaning the first element of the array has an index of 0, the second has an index of 1, and so on.
  • Traversal of a single-dimensional array can be accomplished using a looping construct such as a for loop.
  • A common operation on single-dimensional arrays is sorting, which arranges elements in a certain order (ascending or descending).

Multi-Dimensional Arrays

  • A multi-dimensional array is a data structure that can store data in a matrix form consisting of rows and columns.
  • A two-dimensional (2D) array is the simplest form of multi-dimensional arrays. It is equivalent to an array of single-dimensional arrays.
  • Multi-dimensional arrays can be visualised as an array of arrays, or ‘a table with rows and columns’.
  • Just like the single dimensional array, each element in a multi-dimensional array is identified with an index, which is actually a set of indices. Each set corresponds to a row and a column.
  • A common way to traverse a multi-dimensional array is by using nested loops. The outer loop typically represents rows and the inner loop represents columns.
  • Multi-dimensional arrays can be applied in numerous real-world scenarios, such as representation of a chess board (8 x 8), or storing an image (which has width and height, and sometimes layers for colours).
  • When dealing with multi-dimensional arrays, it is important to understand the concept of row-major order and column-major order, which are methods for storing multi-dimensional arrays in linear storage like memory.