An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The diagrammatic representation of the Array is given below:
Program 1: Below is an illustration of a 1D array:
C++
// C++ program to illustrate 1D array
#include <bits/stdc++.h>
usingnamespacestd;
// Driver Code
intmain()
{
// Given array
intarr[] = { 6, 10, 5, 0 };
// Print the array elements
for(inti = 0; i < 4; i++) {
cout << arr[i] << " ";
}
return0;
}
Java
// Java program to illustrate 1D array
classGFG{
// Driver Code
publicstaticvoidmain(String[] args)
{
// Given array
intarr[] = { 6, 10, 5, 0};
// Print the array elements
for(inti = 0; i < 4; i++)
{
System.out.print(arr[i] + " ");
}
}
}
// This code is contributed by Rohit_ranjan
Python3
# Python3 program to illustrate 1D array
# Driver Code
if__name__ =='__main__':
# Given array
arr =[6, 10, 5, 0];
# Print the array elements
fori inrange(0, 4):
print(arr[i], end =" ");
# This code is contributed by Rohit_ranjan
C#
// C# program to illustrate 1D array
usingSystem;
classGFG{
// Driver Code
publicstaticvoidMain(String[] args)
{
// Given array
int[] arr = {6, 10, 5, 0};
// Print the array elements
for(inti = 0; i < 4; i++)
{
Console.Write(arr[i] + " ");
}
}
}
// This code is contributed by Rajput-Ji
Javascript
<script>
// Javascript program to illustrate 1D array
// Given array
let arr = [6, 10, 5, 0];
// Print the array elements
for(let i = 0; i < 4; i++)
{
document.write(arr[i] + " ");
}
// This code is contributed by divyeshrabadiya07.
</script>
Output:
6 10 5 0
Program 2: Below is an illustration of a 2D array:
C++
// C++ program to illustrate 1D array
#include <bits/stdc++.h>
usingnamespacestd;
// Driver Code
intmain()
{
// A 2D array with 3 rows and
// 2 columns
intx[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
// Print each array element value
// Traverse row
for(inti = 0; i < 3; i++) {
// Traverse column
for(intj = 0; j < 2; j++) {
// Print element
cout << "Element at x["<< i
<< "]["<< j
<< "]: ";
cout << x[i][j] << endl;
}
}
return0;
}
Java
// Java program to illustrate 1D array
importjava.util.*;
classGFG{
// Driver Code
publicstaticvoidmain(String[] args)
{
// A 2D array with 3 rows and
// 2 columns
intx[][] = { { 0, 1}, { 2, 3}, { 4, 5} };
// Print each array element value
// Traverse row
for(inti = 0; i < 3; i++)
{
// Traverse column
for(intj = 0; j < 2; j++)
{
// Print element
System.out.print("Element at x["+ i +
"]["+ j + "]: ");
System.out.print(x[i][j] + "\n");
}
}
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program to illustrate 1D array
# Driver Code
if__name__ =='__main__':
# A 2D array with 3 rows and
# 2 columns
x =[[0, 1], [2, 3], [4, 5]];
# Print each array element value
# Traverse row
fori inrange(3):
# Traverse column
forj inrange(2):
# Print element
print("Element at x[", i ,
"][", j , "]: ", end ="");
print(x[i][j]);
# This code is contributed by sapnasingh4991
C#
// C# program to illustrate 1D array
usingSystem;
classGFG{
// Driver Code
publicstaticvoidMain(String[] args)
{
// A 2D array with 3 rows and
// 2 columns
int[,]x = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
// Print each array element value
// Traverse row
for(inti = 0; i < 3; i++)
{
// Traverse column
for(intj = 0; j < 2; j++)
{
// Print element
Console.Write("Element at x["+ i +
","+ j + "]: ");
Console.Write(x[i,j] + "\n");
}
}
}
}
// This code is contributed by Princi Singh
Javascript
<script>
// JavaScript program to illustrate 1D array
// A 2D array with 3 rows and
// 2 columns
let x = [ [ 0, 1 ], [ 2, 3 ], [ 4, 5 ] ];
// Print each array element value
// Traverse row
for(let i = 0; i < 3; i++)
{
// Traverse column
for(let j = 0; j < 2; j++)
{
// Print element
document.write("Element at x["+ i +
"]["+ j + "]: ");
document.write(x[i][j] + "</br>");
}
}
</script>
Output:
Element at x[0][0]: 0
Element at x[0][1]: 1
Element at x[1][0]: 2
Element at x[1][1]: 3
Element at x[2][0]: 4
Element at x[2][1]: 5
Map:
A map is an associative container that stores elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have equal key values.
The diagrammatic representation of Map is given below:
Program 1: Below is an illustration of a map:
C++
// C++ program to illustrate Map
#include <bits/stdc++.h>
usingnamespacestd;
// Driver Code
intmain()
{
// Empty map container
map<int, int> gquiz1;
// Insert elements in Map
gquiz1.insert(pair<int, int>(1, 40));
gquiz1.insert(pair<int, int>(2, 30));
gquiz1.insert(pair<int, int>(3, 60));
// Iterator to iterate Map
map<int, int>::iterator itr;
cout << "\nThe map gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
// Print map gquiz1
for(itr = gquiz1.begin();
itr != gquiz1.end(); ++itr) {
cout << '\t'<< itr->first
<< '\t'<< itr->second
<< '\n';
}
return0;
}
Java
// Java program to illustrate Map
importjava.util.*;
classGFG{
// Driver Code
publicstaticvoidmain(String[] args)
{
// Empty map container
HashMap<Integer,
Integer> gquiz1 = newHashMap<Integer,
Integer>();
// Insert elements in Map
gquiz1.put(1, 40);
gquiz1.put(2, 30);
gquiz1.put(3, 60);
// Iterator to iterate Map
Iterator<Map.Entry<Integer,
Integer>> itr = gquiz1.entrySet().
iterator();
System.out.print("\nThe map gquiz1 is : \n");
System.out.print("KEY\tELEMENT\n");
// Print map gquiz1
while(itr.hasNext())
{
Map.Entry<Integer,
Integer> entry = itr.next();
System.out.print('\t'+ entry.getKey()
+ "\t"+ entry.getValue()+ "\n");
}
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program to illustrate Map
# Driver Code
if__name__ =='__main__':
# Empty map container
gquiz1 =dict()
# Insert elements in Map
gquiz1[1] =40
gquiz1[2] =30
gquiz1[3] =60
print("\nThe map gquiz1 is : ")
print("KEY\tELEMENT")
forx, y ingquiz1.items():
print(x, "\t", y)
# This code is contributed by Rajput-Ji
C#
// C# program to illustrate Map
usingSystem;
usingSystem.Collections.Generic;
classGFG{
// Driver Code
publicstaticvoidMain(String[] args)
{
// Empty map container
Dictionary<int,
int> gquiz1 = newDictionary<int,
int>();
// Insert elements in Map
gquiz1.Add(1, 40);
gquiz1.Add(2, 30);
gquiz1.Add(3, 60);
Console.Write("\nThe map gquiz1 is : \n");
Console.Write("\tKEY\tELEMENT\n");
// Print map gquiz1
foreach(KeyValuePair<int,
int> entry ingquiz1)
{
Console.Write("\t"+ entry.Key +
"\t"+ entry.Value + "\n");
}
}
}
// This code is contributed by Amit Katiyar
Output:
The map gquiz1 is :
KEY ELEMENT
1 40
2 30
3 60
Program 2: Below is an illustration of an unordered map:
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy