Skip to content
Related Articles

Related Articles

Introduction to Arrays

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 22 Jun, 2022

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The base value is index 0 and the difference between the two indexes is the offset.
For simplicity, we can think of an array as a fleet of stairs where on each step is placed a value (let’s say one of your friends). Here, you can identify the location of any of your friends by simply knowing the count of the step they are on. 
Remember: “Location of next index depends on the data type we use”. 

array

The above image can be looked at as a top-level view of a staircase where you are at the base of the staircase. Each element can be uniquely identified by its index in the array (in a similar way as you could identify your friends by the step on which they were on in the above example). 

Array’s size

In C language, the array has a fixed size meaning once the size is given to it, it cannot be changed i.e. you can’t shrink it nor can you expand it. The reason was that for expanding if we change the size we can’t be sure ( it’s not possible every time) that we get the next memory location to us for free. The shrinking will not work because the array, when declared, gets memory statically allocated, and thus compiler is the only one that can destroy it.
Types of indexing in an array: 

  • 0 (zero-based indexing): The first element of the array is indexed by a subscript of 0.
  • 1 (one-based indexing): The first element of the array is indexed by the subscript of 1.
  • n (N-based indexing): The base index of an array can be freely chosen. Usually, programming languages allowing n-based indexing also allow negative index values, and other scalar data types like enumerations, or characters may be used as an array index.

C++




#include <iostream>
using namespace std;
 
int main()
{
    // Creating an integer array
    // named arr of size 10.
    int arr[10];
    // accessing element at 0 index
    // and setting its value to 5.
    arr[0] = 5;
    // access and print value at 0
    // index we get the output as 5.
    cout << arr[0];
    return 0;
}


C




#include <stdio.h>
 
int main()
{
 
    // Creating an integer array
    // named arr of size 10.
    int arr[10];
    // accessing element at 0 index
    // and setting its value to 5.
    arr[0] = 5;
    // access and print value at 0
    // index we get the output as 5.
    printf("%d", arr[0]);
 
    return 0;
}


Java




class GFG
{
    public static void main (String[] args)
    {   
       
      // Creating an integer array
      // named arr of size 10.
      int[] arr = new int[10];
       
      // accessing element at 0 index
      // and setting its value to 5.
      arr[0] = 5;
       
      // access and print value at 0
      // index we get the output as 5.
      System.out.println(arr[0]);   
           
    }
}
 
// This code is contributed by muditj148.


Output

5

Here the value 5 is printed because the first element has index zero and at the zeroth index, we already assigned the value 5.

Types of arrays : 

  1. One dimensional array (1-D arrays)
  2. Multidimensional array

Advantages of using arrays: 

  • Arrays allow random access to elements. This makes accessing elements by position faster.
  • Arrays have better cache locality which makes a pretty big difference in performance.
  • Arrays represent multiple data items of the same type using a single name.

Disadvantages of using arrays: 
You can’t change the size i.e. once you have declared the array you can’t change its size because of static memory allocation. Here Insertion(s) and deletion(s) are difficult as the elements are stored in consecutive memory locations and the shifting operation is costly too.
Now if take an example of the implementation of data structure Stack using array there are some obvious flaws. 
Let’s take the POP operation of the stack. The algorithm would go something like this. 

  1. Check for the stack underflow
  2. Decrement the top by 1

What we are doing here is, that the pointer to the topmost element is decremented, which means we are just bounding our view, and actually that element stays there taking up the memory space. If you have an array (as a Stack) of any primitive data type then it might be ok. But in the case of an array of Objects, it would take a lot of memory.

Examples – 

// A character array in C/C++/Java
char arr1[] = {‘g’, ‘e’, ‘e’, ‘k’, ‘s’};

// An Integer array in C/C++/Java
int arr2[] = {10, 20, 30, 40, 50};

// Item at i’th index in array is typically accessed as “arr[i]”.  
For example:
arr1[0] gives us ‘g’
arr2[3] gives us 40

Usually, an array of characters is called a ‘string’, whereas an array of ints or floats is simply called an array.

Applications on Array

  • Array stores data elements of the same data type.
  • Arrays are used when the size of the data set is known.
  • Used in solving matrix problems.
  • Applied as a lookup table in computer.
  • Databases records are also implemented by the array.
  • Helps in implementing sorting algorithm.
  • The different variables of the same type can be saved under one name.
  • Arrays can be used for CPU scheduling.
  • Used to Implement other data structures like Stacks, Queues, Heaps, Hash tables, etc.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!