Arrays

Understanding Arrays

What is an Array?

  • An array is a container object that holds a fixed number of values of a single type.
  • They are declared with a type and an identifier, followed by square brackets [] which may also contain a size. For example, int marks[10];
  • The size is decided at the time of creation and cannot be changed.

Accessing Array Elements

  • An array is essentially a collection of variables, placed in contiguous memory locations, that can be individually accessed by indexing.
  • Array indexing starts from zero (0), not one. For instance, to get the first element in an array named ‘marks’, you would write marks[0];
  • Likewise, to access the nth element, you would write marks[n-1];

Multidimensional Arrays

  • Arrays can have more than one dimension, making them multidimensional. For instance, a grid can be represented as a two-dimensional array.
  • They are declared similarly to one-dimensional arrays, but with additional sets of brackets for each additional dimension. For example, int grids[10][10];

Array Manipulations

  • You can modify array values by using their indices. For example, marks[0] = 90; would replace the first element of the array ‘marks’ with 90.
  • Iterating an array means running through each element of an array. This can be done using a loop structure.

Key Takeaway

  • Arrays are powerful constructs that allow us to hold and manipulate large sets of data of the same type. They are essential in many processes, such as sorting and searching data, or working with matrices. Understanding arrays well is a key element to becoming proficient in programming.