Arrays

  • The array is a collection of the same type of elements at contiguous memory locations under the same name. 
  • It is easier to access the element in the case of an array. 
  • The size is the key issue in the case of an array which must be known in advance so as to store the elements in it. 
  • Insertion and deletion operations are costly in the case of an array since the elements are stored at contiguous memory locations. 
  • No modification is possible at the runtime after the array is created and memory wastage can also occur if the size of the array is greater than the number of elements stored in the array.

Array representation:

C++




// C++ code for creating an array
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
   
    // Creating an array
    int arr[10]={1,2,3,4,5,6,7,8,9,10};
   
    // Printing the array
    for (int i = 0; i < 10; i++)
    {
        cout << arr[i] << " ";
    }
    return 0;
}


Java




/*package whatever //do not write package name here */
import java.io.*;
 
class GFG {
  public static void main (String[] args) {
 
    // Creating an array
    int arr[] = {1,2,3,4,5,6,7,8,9,10};
 
    // Printing the array
    for (int i = 0; i < 10; i++){
      System.out.print(arr[i]+" ");
    }
  }
}
 
// This code is contributed by aadityaburujwale.


Python




# Python code for Creation of Array
 
# importing "array" for array creation
import array as arr
 
# creating an array with integer type
a = arr.array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
 
# printing the array
for i in range(0, 10):
   
    # comma is used to generate spaces
    print(a[i]),


C#




// C# code for creating an array
using System;
 
// Driver Code
public class GFG {
 
    static public void Main()
    {
       
        // Declaring an array
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      
        // Printing the array
        for (int i = 0; i < arr.Length; i++) {
            Console.Write(arr[i] + " ");         
        }
    }
}


Javascript




<script>
     
    // Creating an array
    let arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
     
    // Printing the array
    for(let i = 0; i < 10; i++){
        document.write(arr[i] + " ");
    }
     
    // This code is contributed by lokesh
     
</script>


Output

1 2 3 4 5 6 7 8 9 10 

Time Complexity: O(1)
Auxiliary Space: O(1)

Differences between Array and Dictionary Data Structure

Similar Reads

Arrays:

The array is a collection of the same type of elements at contiguous memory locations under the same name.  It is easier to access the element in the case of an array.  The size is the key issue in the case of an array which must be known in advance so as to store the elements in it.  Insertion and deletion operations are costly in the case of an array since the elements are stored at contiguous memory locations.  No modification is possible at the runtime after the array is created and memory wastage can also occur if the size of the array is greater than the number of elements stored in the array....

Dictionary:

...

Comparison Between Array and Dictionary:

...

Contact Us