A comprehensive guide on Arrays- DS, Java, C, Python

What are Arrays

An Array is a collection of data with similar data types stored in a contiguous memory location. Data types can be an integer, float, String, etc. An array is used in computer programming to sort pr search the data easily. We can retrieve the data by their indexing. Let’s say we have n elements in an array, and the indexing starts with 0 and goes till n-1 position in an incremental manner. The difference between the two indexes is called offset. Please refer to the below image.

Example of Array

Arrays Example
Arrays

Usages of Array

  1. Let’s say we need 100 variables in a program. Is it feasible to declare 100 variables like v1,v2,v3,….,v100? Instead, we can use an array, like v[0],v[1],…,v[100], which will do the same work.
  2. An array can be used in a place where we need homogeneous data types like library management systems.
  3. An algorithm like searching, sorting uses Array

Array Properties

  1. An array stores homogeneous data types, and the size of the data is the same. For example, if we store the integer data type, all the data must be an integer, and its size is 4.
  2. Data is stored in a contiguous memory location.
  3. We can retrieve the data by their index.

Array Advantages

  1. Its index can directly access any element in an Array. For example, if we use air[0], it will return the element of the 0th position. Similarly are[5] will return the element of the 5th position in the array.
  2. Traversing through an array is very easy as we only increment the index. Suppose any Array is starting with 0th position and has n numbers of the value we need to increment the value till n-1th position.

Types of indexing available in Array

  1. 0- based indexing: The first element of the array will start with the zeroth position. For Array A, the first element will A[0], and if it has n numbers, the last element will be A[n-1]
  2. 1 – based indexing: The first element of the array will start with 1st position. For Array A, the first element will A[1], and if it has n numbers, the last element will be A[n]
  3. N – based indexing: The first element can be stored in any random index.
Array memory allocation

Types of Arrays

There are mainly two types of Array available.

Time Complexity of Array

OperationsAvg CaseWorst Case
Access opsO(1)O(1)
SearchingO(n)O(n)
Insert opsO(n)O(n)
Delete opsO(n)O(n)

Now we will discuss how to declare Array in Java, Python, and C

How to declare arrays in Java

To declare the linear or one-dimensional array, please follow the below process.

/To declare the array
String[] name;

//To declare and assign the value
String[] name={"Paul","Adam","Fatima","Ricky"};

//To create a integer array the defined data type must be an integer. Please see below:
int[] num={10,15,20,30,35,40};

//To declare the array dynamically
int[] num=new int[10];

//To assign a value for any index
num[0]=10;
num[1]=20; 

How to declare Arrays in Python

#To declare and assign the value
 name=["Paul","Adam","Fatima","Ricky"]

#To assign any value
name[0]="XYZ"

Note: Python does not have any predefined library for an array. We can use a third-party library like NumPy. In this article, we have used a python list as an array.

How to declare Arrays in C

//To declare and assign value
int num[5]={0,5,11,16,21};

Now we will discuss how to access array elements in Java, python and C

How To access Arrays element in Java

//To declare and assign the value
String[] name={"Paul","Adam","Fatima","Ricky"};
//To access array element
System.out.println(name[0]);

How To access Arrays element in Python

#To declare and assign the value
name=["Paul","Adam","Fatima","Ricky"]
#To access the value
var=name[0]

How To access Arrays element in C

//To declare and assign value
int num[5]={0,5,11,16,21};
//To access the value
printf("%d\n", values[0]);

Now we will discuss how to use array length in Java, python and C

How to use Arrays Length in Java

String[] name={"Paul","Adam","Fatima","Ricky"};
//to get the length
int size=name.length;
System.out.printn(size);

How to use Arrays Length in Python

#To declare and assign the value
name=["Paul","Adam","Fatima","Ricky"]
 size= len(name)

How to use Arrays Length in C

//To declare and assign value
int num[5]={0,5,11,16,21};
printf("Size of int array:%d \n",sizeof(num)/sizeof(num[0]));

Now we will discuss how to loop in array in Java, python and C

How to use a loop in Array in Java

public static void main(String[] args){
        int[] num=new int[10];
        //To declare and assign the value
        String[] name={"Paul","Adam","Fatima","Ricky"};
        System.out.println(name[0]);
        //for loop
        for(int i=0;i<name.length;i++){
            System.out.print(name[i]);
        }

How to use loop in Array in Python

name=["Paul","Adam","Fatima","Ricky"]
for i in name :
    print(i)

How to use a loop in Array in C

#include <stdio.h>

int main()
{
    //To declare and assign value
    int num[5]={10,16,21,26,31};
    for(int i=0;i<5;i++){
       printf("%u\n", num[i]);
    }
    
    

    return 0;
}

Some important Array interview Questions

Qn 1: Can we change the Array size in Java?

Ans: No, we can not change the Array size. Once it is created, the size of the Array is fixed. If you have to change the size, you need to use ArrayList.

Qn 2. What is ArrayStoreExcpetion ?

Ans: Array stores similar data type. If we try to store some other data type in an array it gives an exception “ArrayStoreExcpetion.” For example:

public static void main(String[] args){
      
        int[] num=new int[5];
        num[0]=12.5;//Compile time Exception

    }

 public static void main(String[] args){

        Object[] num=new String[5];
        //num[0]=12.5;//Compile time Exception
        num[1]=new Double(12.5); //Runtime Exception java.lang.ArrayStoreException: java.lang.Double

    }

Qn 3. what is ArrayIndexOutOfBoundsException ?

Ans: ArrayIndexOutOfBoundsException comes if users try to access an array index that is negative or more than the array. If the Array index starts with 0 and size is length, anything less than 0 and greater than length-1 will give ArrayIndexOutOfBoundsException.

Qn 4. What is the difference between ArrayStoreExcpetion and ArrayIndexOutOfBoundsException ?

Ans: Both Exceptions throws at runtime. ArrayStoreExcpetion throws when data type does not match Array data type, whereas ArrayIndexOutOfBoundsException throws when accessing index is not in the range.

Qn 5. How to initialize an array with array Size in Java?

Ans:

 int[] num=new int[5];//5 is the Array size

Qn 6. How to initialize an array with predefined value?

Ans:

int[] num={0,5,11,16,21};

Qn 7. How to sort an array in Java?

Ans: Using Arrays’ predefined method Arrays.sort(), we can sort the array.

public static void main(String[] args){

        int[] num={0,5,11,16,21};
        Arrays.sort(num);
        for(int i=0;i<num.length;i++){
            System.out.println(num[i]);
        }


    }

Qn 8. How to convert an array to a String ?

Ans: Using Arrays’ predefined method Arrays.toString(), we can sort the Array.

public static void main(String[] args){

        int[] num={0,5,11,16,21};
        System.out.print(Arrays.toString(num));

    }

Qn 9. How to copy an array?

Ans: Using System.arrayCopy(), users can copy the Array. Otherwise, then can manually copy the Array iterating the array elements.

Qn 10. Can Generics be used in an array?

Ans. No we can not use Generics with Array.

Qn 12. What are the disadvantages of an array?

Ans:

  1. The size of array is static we can not change the size once initialized.
  2. Operations like insertion and deletion are difficult to perform in array.
  3. If the user defines more memory than the required, excess memory gets wasted.
  4. The data type used in array should be the same. For example, if we use int as a data type, we can not store float, String in the same array. It has to be always int only.

Conclusion

Till now, We have covered the basics of Arrays. In the next topic, we will write about some important problems and solutions of Array. To go through our full tutorials on Data structures, please click this link. For more details for this section, please refer to this link.

LOW SUPERHEAT | IMPORTANT TERMS & 3 FAQS

Content DEFINITION OF LOW SUPERHEAT | LOW SUPERHEAT CAUSESLOW SUPERHEAT LOW SUBCOOLING | CAUSES, AND REMEDY FOR LOW SUPERHEAT AND LOW SUBCOOLING | LOW SUPERHEAT LOW SUBCOOLING 410ALOW SUPERHEAT NORMAL SUBCOOLINGHOW TO RAISE OR LOWER SUPERHEAT?LOW DISCHARGE SUPERHEAT ALARM | CAUSES OF LOW DISCHARGE SUPERHEAT | WHAT CAUSES LOW DISCHARGE SUPERHEATA LOW EVAPORATOR SUPERHEAT INDICATESHIGH…

Continue Reading LOW SUPERHEAT | IMPORTANT TERMS & 3 FAQS

Puppeteer Browser Class – An Excellent Learning Guide of Puppeteer Tutorial 8

Puppeteer is an open-source node js library and is used a web automation as well as web scraping tool. You need the basic understanding of Javascript, and HTML DOM structure to start working with Puppeteer. This Puppeteer tutorial series is distributed in the below segments which will equip you with all the necessary experience to…

Continue Reading Puppeteer Browser Class – An Excellent Learning Guide of Puppeteer Tutorial 8

Puppeteer Class – An Excellent Learning Guide of Puppeteer Tutorial 7

Puppeteer which is an open-source node js library, can be used as a web scraping tool. Understanding of command line, Javascript, and HTML DOM structure should be good to start with this puppeteer tutorial. The Series of Puppeteer tutorial is distributed among below Sub section to get a good hold on Puppeteer.  Puppeteer Tutorial Tosca…

Continue Reading Puppeteer Class – An Excellent Learning Guide of Puppeteer Tutorial 7

Tosca Automation Tool – An Excellent Learning Guide for Tosca 13.x

Tosca Tutorial – Table of Content Tosca Tutorial #1: Tosca Overview Tosca Tutorial #2: Tosca Automation Overview Tosca Tutorial #3: Tricentis Tosca Setup – Install, Uninstall and License Configuration Tosca Tutorial #4: Tosca Workspace Creation Tosca Tutorial #5: Understanding of TOSCA Commander and Tosca User Management Tosca Tutorial #6: Tosca Scanning – An Introduction to Modules Tosca Tutorial #7: Tosca Test Case Creation Tosca Tutorial #8: Tosca Parameters and Library– Buffer, Business Parameter, TCP Tosca Tutorial #9:Tosca Test…

Continue Reading Tosca Automation Tool – An Excellent Learning Guide for Tosca 13.x

Puppeteer Automation Testing – An Excellent Guide of Puppeteer Tutorial 6

The Puppeteer is a node js library based framework which is available as open-source. It can be used for web scraping tools. It’s also used for test automation tools as well. Now-a-days, the usage of Puppeteer is getting increased rapidly in the automated software testing space. Basic knowledge of command line, Javascript, and HTML DOM…

Continue Reading Puppeteer Automation Testing – An Excellent Guide of Puppeteer Tutorial 6

Install Puppeteer – An Excellent Learning Guide of Puppeteer Tutorial 4 & 5

Puppeteer is an open-source node js library that can be used for web scraping tools. It can also be used to perform test automation in web applications. Now-a-days, the usage of Puppeteer is getting increased rapidly in the automated software testing space. Basic knowledge of command line, Javascript, and HTML DOM structure is required to…

Continue Reading Install Puppeteer – An Excellent Learning Guide of Puppeteer Tutorial 4 & 5

About Himadri Das

Hi, I am Himadri Das, I am a blogger, and an open source contributor. I have about 11 years of experience in the Information Technology domain. Currently I am working in a Startup Company as Quality Assurance Manager. I have hands-on experience on Appium, Selenium, QTP, Locust, Automation framework, Performance Testing, Functional Testing, Java, python, Shell scripting, MySql, Redis, Kafka etc. Apart from my work and writing blogs, I love to play guitar, love to travel and love to watch cricket and football. If you want to know more about me, please visit my linkedin profile - https://www.linkedin.com/in/himadri-das-2b6b7421/.