- 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++
#include <iostream>
using namespace std;
int main()
{
int arr[10]={1,2,3,4,5,6,7,8,9,10};
for ( int i = 0; i < 10; i++)
{
cout << arr[i] << " " ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
for ( int i = 0 ; i < 10 ; i++){
System.out.print(arr[i]+ " " );
}
}
}
|
Python
import array as arr
a = arr.array( 'i' , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ])
for i in range ( 0 , 10 ):
print (a[i]),
|
C#
using System;
public class GFG {
static public void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for ( int i = 0; i < arr.Length; i++) {
Console.Write(arr[i] + " " );
}
}
}
|
Javascript
<script>
let arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
for (let i = 0; i < 10; i++){
document.write(arr[i] + " " );
}
</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
Contact Us