Amazon Prime

  • Data Structure
  • Array

    Array Data Structure


    Array is a linear collection of data of the same type stored in contiguous memory locations. An array always holds a fixed number of elements in it. The elements of the array are accessed using the position or index of the element, ranging from 0 to n-1 (where n is the size or length of the array).

    To declare an array, we should know the data type of elements and size of the array beforehand. The syntax of array declaration varies from one programming language to another.

    Array in Java

    In Java, declaration of array mainly comprises 3 sections.

    • Array Name : Name of the array to identify.
    • Data Type : It can be primitive data type like int, double etc or any user defined data type.
    • Array Size : Max capacity of the array.

    Array of 10 Elements


    For instance if we want to store age of 100 students, we declare age array as

    1int[] age = new int[100] ;


    Once an array is declared, we can initialize values of the array using index.

    1// Array Declaration
    2int[] age = new int[5];
    4// Array Initialization
    5age[0] = 15;
    6age[1] = 14;
    7age[2] = 16;
    8age[3] = 16;
    9age[4] = 15;
    11In a single statement we can declare and initialize values
    12// Array declaration and initialization
    13int[] age = 15;

    Accessing Elements

    To access array elements index of the particular element is used, the index range is always between [0, n-1] where n is the length of the array.

    1// to access array element at index 0
    4// to access array element at index 4

    Java Code

    1public class ArrayDeclaration {
    3 public static void main(String[] args) {
    4 // Declaration of an array
    5 // Declaration array with type and size
    6 int[] a1 = new int[5];
    8 // assigning values to array elements using index
    9 a1[0] = 10;
    10 a1[1] = 20;
    11 a1[2] = 30;
    12 a1[3] = 40;
    13 a1[4] = 50;
    15 // Declaration and Initialization in single line
    16 int[] a2 = {10, 20, 30, 40, 50};
    19 // accessing array elements and printing their values
    20 for (int i = 0; i < a1.length; i++) {
    21 System.out.print(a1[i]);
    22 }
    23 System.out.println();
    24 }

    Advantages and Disadvantages

    To understand the advantages/disadvantages of arrays more clearly, let us assume we need to store student names who are enrolled for a course named “Mathematics In Computing" in an array alphabetically and the max capacity of the students is fixed and is 15. At the moment only 5 students have been enrolled and the names of the students in alphabetic order are "Alex", "Bob", "Danie", "Roxie" and "Tony".

    1String[] names = new String[15];
    2names[0] = "Alex" ;
    3names[1] = "Bob";
    4names[2] = "Danie";
    5names[3] = "Roxie";
    6names[4] = "Tony";



    Student Names Array


    • In arrays, data is stored in contiguous memory locations so no extra space is needed to link between array elements.
    • As the size of the array is specified during initialization, memory is allocated during compile time and so we will not face any space issues later.
    • In arrays, elements are accessed using index, so random access of the elements is possible by using index and to access an element takes O(1) time complexity which is very efficient. To get the student name at 3rd index, we can simply use names[3].
    • Array is the key data structure used to create data structures like ArrayList, Queue, Stack which are dynamic in size.


    • Size of the array must be specified during array initialization (Static memory allocation), so it is not possible to increase or decrease array size after declaration. That means if we want to increase the max capacity of students to enroll for the course "Mathematics In Computing" is not possible.
    • Sometimes even if we declare an array with max capacity as n, we might not use total capacity, in these cases wastage of memory occurs. Suppose 7 students enrolled for the course "Mathematics In Computing", and we have declared max capacity as 15, so half of the memory almost gets wasted.
    • If a new student named Carmen enrolled for the course “Mathematics In Computing", to maintain alphabetic order of the students, the correct position of Carmen should be at index 2, that means all elements from index 2 onwards have to be moved one position ahead. So insertion at random index takes O(n) in the worst case.

    Arrays Add Operation

    • If any student wants to withdraw from the course, we need to remove the student name from the names array. For example if Alex wants to withdraw from the course, we need to shift all values from index 1 to one position behind. This operation also takes O(n) time in the worst case.

    Arrays Delete Operation




  • Data Structure
  • Array