HomeBlogProgrammingWhat is an Array? - Introduction with Examples

What is an Array? - Introduction with Examples

26th Apr, 2024
view count loader
Read it in
18 Mins
In this article
    What is an Array? - Introduction with Examples

    We belong to an era where it is impossible to long for a progressive world without Information Technology. And when we talk about Information Technology, Computer Programming readily comes into our mind. An Array is one of the essential facets of programming languages and software development. This is because an array is of great utility in maintaining extensive datasets, sorting, and identifying variables. 

    In its simplest terms, an array can be defined as a formation of digits, images, or objects arranged in rows and columns in accordance with their types. Comprehending the concept of array facilitates your tedious work and allows you to make it concise as arrays illustrate numerous data elements of similar type using a sole name. Explore more to understand what is an array, the array concepts, and the features of the array with KnowledgeHut’s Computer Programming Course. 

    What Is Array?

    In Computer Programming, an array assembles equivalent data elements stored at adjacent memory locations. This easiest data structure helps to determine the location of each piece of data by simply using an offset to the base value. An offset is a number that illustrates the distinction between two index numbers 

    For instance, If you want to keep a record of the marks obtained by a student in 6 subjects, then you don’t need to determine the variables individually. Rather, you can determine an array that will store each piece of data factors at a contiguous memory location. 

    Arrays are one of the oldest and most essential data structures used in almost every program. Arrays are also useful in implementing other data structures such as lists, heaps, hash tables, deques, queues, stacks, and strings.

    Concept of Array

    Explain Array in Different Programming

    The term ‘array’ can also be referred to as a data type that is provided by most high-level programming languages. It consists of a set of variables that can be determined by one or more indices calculated at run-time. However, arrays are interpreted in multiple ways depending on our operating programming language. Let us explain the concept of arrays in different programming languages.

    1. Python

    In Python, the array can be driven by a module called “array,” which is useful in manipulating a single data value type. Element and index are important and useful terms in comprehending ‘what is array’. In Python, the following syntax is used to create an array- 

    Variable _name = array ( type code, [value_list] ) 
    For Example  
    import array as arr 
    myarray = arr. array ( ‘b’, [2, 3, 4, 5, 6]) 

    In the above code, the letter ‘b’ stands for the type code, and the value is an integer type.  

    The array elements can be accessed only by using the index, which is always an integer. The syntax for accessing the array element is 

    variable_name [index_number]  
    For Example 
    import array as ar 
    height = ar. array (‘i’, [145, 146, 144, 166, 134])height = ar.array (‘i’, [145, 146, 144, 166, 134]) 
    print (height[2]) 
    print (height[0]) 



    The above figure shows the array elements and their indexing. As in the array, the indexing starts with a 0, and this instance shows the value at the height [ 2 ] is 144, and at the height [0], the value is 145.  

    Use cases 

    • A Python array can be useful when we use many similar variables. 
    • We can utilize Python arrays when we need to process the data dynamically. 
    • Python arrays use less memory, and hence, they are faster. 

    If you’re looking to update your Python skills to obtain efficiency, KnowlegdeHut’s Python Programming training can work in your favor to get started! 

    2. Java

     In Java, this is how an array is declared- 

    dataType[] arrayName; 

    In Java, the data types can be primal such as double, byte, int, char, etc., and an array acts as an identifier. 

    For Example 

    int[] data;

    Here data is an array, and the int represents the data type. 

    In Java, the array elements can be accessed using their index numbers. Below is the syntax to access the array elements. Well, in Java, the array elements can be accessed by using their index numbers. Below is the syntax to access the array elements. 


    Let us explain the syntax through an example 

    class Main { 
     public static void main(String[] args) { 
    // create an array 
    int[] age = {11, 6, 4, 3, 4}; 
    // access each array components 
    System.out.println("Accessing components of Array:"); 
    System.out.println("First component: " + age[0]); 
    System.out.println("Second component: " + age[1]); 
    System.out.println("Third component: " + age[2]); 
    System.out.println("Fourth component: " + age[3]); 
    System.out.println("Fifth component: " + age[4]); 


    Accessing Elements of Array: 
    First Element: 11 
    Second Element: 6 
    Third Element: 4 
    Fourth Element: 3 
    Fifth element: 4  

    Use case 

    • In times of using numerous values in a single variable, Java arrays can be helpful 
    • These arrays are comparatively faster than the primeval data types 
    • They are useful in storing objects. 

    3. Java Script

    In Javascript, an array is the sole variable used to store various factors. It is helpful for storing a list of elements while accessing them by a single variable. However, arrays can be determined and initialized in two different ways in Javascript, one is Array Literal syntax, and the other is Array Constructor Syntax.

    Array Literal syntax 

    var <array-name> = [element0, element1, element2,... elementN]; 
    The following is an example of Array Literal Syntax 
    var string array = ["four", "five", "six"]; 
     var numeric array = [4, 5, 6, 7]; numeric array = [4, 5, 6, 7]; 
     var decimalArray = [1.4, 1.5, 1.6];  
    var boolean array = [true, false, false, true]; true];  
    var mixed array = [4, "five", "six", 7];"six", 7]; 

    Array constructor Syntax 

    An array can be initialized with Array Constructor Syntax by using a New keyword. 

    var arrayName = new Array(); 
    var arrayName = new Array(Number length); 
    var arrayName = new Array(element1, element2, element3,... elements); 

    The above instance can be facilitated through an example 

    var stringArray = new Array();  
    stringArray[0] = "two";  
    stringArray[1] = "three";  
    stringArray[2] = "four";  
    stringArray[3] = "five";  
    var numericArray = new Array(3);  
    numericArray[0] = 2;  
    numericArray[1] = 3;  
    numericArray[2] = 4;  
    var mixedArray = new Array(2, "three", 5, "four"); 

    Use case 

    • The arrays in Javascript are resizable and useful in containing a mix of various datatypes. 
    • Javascript arrays are useful in accessing the elements by utilizing their respective string form as indexes. 
    • Javascript arrays are useful in the case of storing components and accessing them through a single variable.

    4. PHP

    In PHP, arrays can be interpreted in three ways. They are  

    • Indexed arrays 
    • Associative Arrays 
    • Multi-dimensional Arrays 

    However, here we have given the syntax of creating an Associative Array has been shown with the help of an example 

    array(key=>value, key=>value, key=>value, etc.) 
    echo "Peter is " . $age['Peter'] . " years old."; 

    Use case 

    • PHP arrays use less code, and hence they are time-savers. 
    • PHP arrays do not require defining multiple variables. 
    • PHP arrays make the sorting process easy 
    • By utilizing a single loop, all the elements of PHP arrays can be easily traversed. 

    5. C

    In C programming, an array is a variable that stores values in multiple ways. Here is the syntax for declaring an array in C programming. 

    dataType arrayName[arraySize];

    The syntax can be explained with the help of an example 

    float mark[7]; 

    Here ‘mark’ represents an array, and the array size is 7, where the data type is ‘float’. The example interprets that the ‘mark’ array can hold 7 floating point values. 

    Use case 

    • C arrays are efficient in accessing all the elements with the help of their index numbers 
    • In C arrays, Matrices are represented through 2D arrays 
    • Applying the search process to an array is a simple process in C arrays

    Arrays in C

    6. C++

    To declare an array in C++, the variable type should be defined, and the name of the array should be specified. Moreover, the number of elements has to be specified. The syntax is as follows- 

    Datatype array name [ size ]; 
    int array3 [9]; 

    Here it represents the data type, and we have declared an array named array3, and 9 refers to the size of the elements. 

    Use case 

    • C++ arrays are useful in storing the obtained datatypes like structures, pointers etc. 
    • C++ arrays are efficient in retrieving the data 
    • C++ arrays are useful for finding any data at an index position. 
    • C#

    In C#, the placement of memories for arrays can be done dynamically. Arrays are basically objects, so it is easy to discover their size with the help of preconceived functions. There is a difference in the way an array works in C# than that of C/C++. This is how we can declare an array in C# 


    < Data Type > [ ] < Name_ Array >  
    int[] x; // can store int values 
    string[] s; // can store string values 
    double[] d; // can store double values 
    Emlpoyee[] Emp1;// can store instances of the Employee class which is custom class 

    The declaration is not enough for placing the memory in an array. We must initialize the array. Following is the syntax for initializing the array 


    Type [ ] < Name Array > = new < datatype > [size]; 


    // defining array with size 7 and assigning // defining array with size 7 and assigning  
    // values at the same time 
    Int [ ] intArray3 = new int [7] {2, 3, 4, 5, 6}; 

    Use case 

    • In C# arrays, the allotment of memory for an array is carried out in an effective way. 
    • In C# arrays, finding out the length of the array is an easy process.

    What Do You Mean by an Array of Structures? Explain With Example

    We now have an understanding of what is an array, but now we will see what an array of structures is. An array of structure typically refers to an array in which each component is a structure of the same type. The reference and subscriptions of the structure arrays go after the same rules as simple arrays. Creating an array of structures is an indispensable part when it comes to the discussion of structure arrays.  

    Creating an array of structures 

    Using the REPLICATE function is the simplest way to create an array of structures. The first criterion to REPLICATE refers to each component's structure. Assuming the STAR structure has been interpreted. An array consisting of 100 elements of structure has been formed with the following declaration. 

    • car = REPLICATE({star}, 100) 
    • car = REPLICATE(A, 100)

    To determine the structure and a structure array in one step, the following statement must be used; 

    • car = REPLICATE({star, name:'', ra:0.0, dec:0.0, $ 
    • intent:FLTARR(12)}, 100)


    The name field of all the 100elements has been set as ‘CLEAR’ 

    car.name = 'CLEAR' 
    Fix the i-th element of car to the contents of the star structure. 
    car[I] = {star, 'BATTELUSE', 11.4, 52.2, FLTARR(12)} 
    ;Store 0.0 into car[0].ra, 1.0 into car[1].ra, ..., 99.0 into 
    car.ra = INDGEN(100) 
    Prints name field of all 100 elements of the car, separated by commas 
    ;(the last field has a trailing comma). 
    PRINT, car. name + ',' 

    Discover the index of the star with the name SURPLUS. 

    I = WHERE(car.name EQ 'SURPLUS') 

    ; Draw out the intensity field from each entry. Q will be a 12 by 100 
    ;floating-point array. 
    Q = car.inten 
    Plot the intensity of the sixth star in array car 

    PLOT, car[5].inten 

    Create a contour plot of the (7,46) floating-point array; extracted from 
    ; months (2:8) and stars (5:50). 

    CONTOUR, car[5:50].inten[2:8] 

    ; Sort the array into ascending order by names. Fix the result 
    ; back into the car. 
    car = car(SORT(car. name)) 
    ; Find out the monthly total intensity of all stars in the array. 
    ; monthly is now a 12-element array. 
    monthly = car.inten # REPLICATE(1,100) 

    Need of Using Array

    Almost all cases in computer programming need to keep track of the extensive amount of similar data. Innumerable variables need to be determined to store such a large amount of data. Needless to say, it is hardly possible to remember the names of all the variables while writing programs. This is where an array comes to our rescue. The following instance determines how an array is of great use when it comes to writing codes. 

    Here in the example, a student has been given marks in 5 different subjects, and the target is to determine the average of the student’s marks.

    Without Using Array

    void main() 
    int subject1 = 50, subject2 = 62, subject3 = 77, subject4 = 80, subject5= 57; 
    float avg = (subject1 + subject2 + subject3 + subject4 + subject5)/5; 
    Using Array h3 
    void main() 
    int subject1 = 50, subject2 = 62, subject3 = 77, subject4 = 80, subject5= 57; 
    float avg = (subject1 + subject2 + subject3 + subject4 + subject5)/5; 

    Types of Indexing in an Array

    Three types of indexing can be found in an array. They are as follows- 

    • The first component of an array is referred to as the 0 indexes in Zero-based indexing 
    • The first component of an array is referred to as 1 index in based indexing 
    • In n-based indexing, the foot index is chosen according to the requirement. 

    How Is an Array Initialized?

     An array can be initialized in different ways. Let us have a look at the following ways in which an array can be initialized. 

    1. Passing No Value Within the Initializer

    This is how the array will be initialized if we continue with an empty initializer list or simply put 0 in the initializer list.

    Int num[7] = { };                      // num = [0, 0, 0, 0, 0, 0, 0]
    Int num[7] = {0};                     // num = [0, 0, 0, 0, 0, 0, 0]

    2. By Passing Specific Values Within the Initializer

    The simplest way to initialize an array is to continue with passing specific values within the initializer.

    Int num [6] = { 2, 3, 4, 5, 6, 7 };

    3. Passing Specific Values Within the Initializer but Not Specifying the Size

    This is how we can initialize an array without specifying while specifying the elements. 

    Int num [ ] = {3, 4, 5 ,6 ,7 ,8, 9};

    4. Universal Initialization

    C++ programming contains a feature called Uniform Initialization that allows the utilization of congenial syntax to initialize values and elements ranging from the primordial type of aggregates. Basically, this initiates brace initialization. The syntax is given below.

    Type var_name{arg1, arg2, ……arg n}

    What Do You Understand by Array Operations

    The array is a type of container that experts in holding large amounts of similar data at adjacent memory locations. Multiple operations can be done with the help of an array in computer programming. Some of them are as follows- 

    • Accessing Elements in Array

    We need the below-given details for accessing the components of an array  

    1. The base address of the array 
    2. Length of a component in bytes 
    3. The type of indexing that the array follows 

    The following formula is used to determine the address of the element of a 1-dimensional array 

    Byte address of component A[i] = base address + length * ( first index- i) 

    • Insertion in an Array

    The prime things that we have to remember while inserting the elements in an array are- 

    1. To enter the length of the array 
    2. To enter the location we want to insert the element 
    3. Then to enter the number we want to insert in that location 
    employee[pos-1]= variable;
    • Searching in an Array

    One of the main operations that can be carried out in an array is searching for an element. The search can be performed to find out the location of the element or to find out whether the element exists. Let us have a look at implementing a binary search in an array. 

    int binarySearch(int arr[], int low, int high, int key) 

    whether (high < low) 
    return -1; 
    int mid = (low + high) / 2; /*low + (high - low)/2;*/ 
    whether (key == arr[mid]) 
    return mid; 
    whether (key > arr[mid]) 
    return binarySearch(arr, (mid + 1), high, key); 
    return binarySearch(arr, low, (mid - 1), key); 
    Driver code */ 
    int main() 
    // Let us search 2 in below array 
    int arr[] = {3 , 4, 5, 6, 7, 8 }; 
    int n, key; 
    n = sizeof(arr) / sizeof(arr[0]); 
    key = 8;
    // Function call 
    cout << "Index: " << binarySearch(arr, 0, n - 1, key) 
    << endl; 
    return 0;

    What Is Array and Its Types

    An array can be defined as a group of elements containing a collection of variables stored under a similar name. An array can be categorized in the following ways- 

    1. Indexed Array

    An index array can be determined as an array that consists of the numeric key. It is fundamentally an array in which each key is connected to its own particular value. 

    2. Associative Array

    An associative array can be termed as an array in which the key can be presented either in numeric or string format. This particular array is kept in the form of key-value pair. 

    3. One-Dimensional Array

    A one-dimensional array is a list of variables consisting of datatypes that come under the same name. In this array, we access all the components just by using their index numbers. A one-dimensional array contains a fixed size. 

    4. Multidimensional Array

    A multi-dimensional array is an array of arrays that holds equivalent data in a table-like form. It is also known as Matrix. Here in the multi-dimensional array, the data is put down in row-major order. 

    Advantages of Using Arrays

    There are several advantages of array. Here, we have mentioned some of them below, Here, we have mentioned some of them below, 

    • Arrays contain numerous homogenous data that come under the same name 
    • We are able to access elements randomly with the help of arrays 
    • Since an array is capable of storing variables at adjacent memory positions, it restricts the shortage of memory and surplus as well. 
    • Arrays can be of great use for storing any type of data that comes with a fixed size. 
    • In the array, if the index is preconceived, it takes a unit of time to access an element. 

    Disadvantages of Using Arrays

    Everything comes with some pros and cons as well. And arrays are no exception. A number of disadvantages can be found in the case of using arrays. Such as, 

    • The length of the array should be predetermined. 
    • Since the array is a constant data structure, it is impossible to modify the length of the array and hence no alterations can be done while we run the codes. 
    • Inserting or deleting any element in an array can be expensive as the variables are kept at adjacent memory locations. 

    Applications of Array

    Here are some applications of arrays: 

    • List of contacts in mobile phones 
    • Matrices utilize arrays for different fields like image processing, computer graphics, and many more 
    • Arrays are also useful for online ticket-booking portals 
    • Since arrays allow faster access to elements, IoT applications utilize arrays 
    • Arrays are helpful for speech processing. Arrays represent each speech signal 
    • The viewing displays of every laptop, as well as desktop, are basically multi-dimensional arrays of pixels.


    We now know what is array, its applications of the array, its advantages of the array, and other aspects. In a nutshell, an array has proved to be of great utility in computer programming as arrays not only help us to save memory they are also useful in terms of reusability and lucidity of codes. Your comprehension of arrays plays a pivotal role while entering into the world of programming languages. 

    To get yourself benefitted by exploring more about arrays and programming, register for KnowledgeHut’s Java Course.

    Frequently Asked Questions (FAQs)

    1Why are arrays used?

    Arrays are used as they are helpful in maintaining huge data sets under a single variable name to get rid of any confusion that can arise while using several variables. 

    2How is data stored in an array?

    To store data in an array, simply cite an array element by using the array's name and index inside brackets. after that, put the assignment operator(=), followed by a value. 

    3How do you master an array?

    An array is popular for containing values at adjacent memory locations. The basics that need to be learned to master arrays are- creating an array, inserting elements into an array, deleting elements from an array, sorting an array, etc. 

    4Which programming is best for jobs?

    Python, Java, and Javascript are the programming languages that top the list. Python is more famous among start-ups, while Java remained preferable among larger organizations for decades. 

    5Is programming a good career?

    Yes, programming is considered to be a good career. It not only offers highly-paid jobs but also provides you with work flexibility. Also, programming comes with ample job opportunities. 


    Ramulu Enugurthi

    Blog Author

    Ramulu Enugurthi, a distinguished computer science expert with an M.Tech from IIT Madras, brings over 15 years of software development excellence. Their versatile career spans gaming, fintech, e-commerce, fashion commerce, mobility, and edtech, showcasing adaptability in multifaceted domains. Proficient in building distributed and microservices architectures, Ramulu is renowned for tackling modern tech challenges innovatively. Beyond technical prowess, he is a mentor, sharing invaluable insights with the next generation of developers. Ramulu's journey of growth, innovation, and unwavering commitment to excellence continues to inspire aspiring technologists.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Your Message (Optional)

    Upcoming Programming Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon