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"

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.

Scroll to Top