Introduction to Arrays

Introduction to Arrays

Published by: Nuru

Published date: 21 Jun 2021

Introduction to Arrays Photo

Introduction to Arrays

As for the introduction to arrays, we say that they are the collection of elements of the same data type placed in a consecutive memory location. We can access these individually using the index giving a unique name.

The individual data items in an array are called elements of the array and they all are of the same data type. This makes it easier to calculate the position of each element by simply adding an offset to a base value. The individual elements are characterized by array names and all of them are followed by one or more indices. We enclose these indices or subscripts in the square brackets. We can store five types of value integers with a single identifier. There is no need of declaring five different variables with a different identifier.

Arrays are very useful when store related data items. For instance, grades of students, marks of students, matrix addition, subtraction, etc. These can be both single-dimensional or multidimensional.

Types of indexing in an array

  • zero-based indexing

In this type, there is the indexing of the first element of the array by the use of subscript of 0.

  • one- based indexing

The subscript 1 indexes the second element of the array in this type.

  • n -based indexing

We can freely choose the n- based index. Usually, programming languages that allow n- based indexing also allow negative index values. It also allows other scalar data types like enumerations or characters that are used in the array.

The most important property of an array is that it stores elements in contiguous memory locations.

Let us take an example:

Int num [5]

Introduction to array

Here, the integer value is within the square bracket. We call it a subscript.

Advantages of Array

  • They represent multiple data items of the same type in a single name.
  • Arrays allow random access of elements which makes accessing elements by position faster.
  • They have a better cache locality and can make a big difference in performance.
  • These also implement matrices.
  • We can use arrays for linking lists.