Example 1: initializing 2d vector
vector<vector<int> > vec( n , vector<int> (m, 0));
Example 2: c fill 2d array
int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
disp[i][j] = disp[i][j];
}
}
Example 3: accessing elements 2D array using pointers
#include<stdio.h>
int main(void){
int arr[4][4]={{1,2,3,4},{5,6,7,8},{9,0,1,2}};
int *ptr = &arr;
for(int i=0;i<3;i++){
for(int j=0;j<4;j++)
printf("%d ",*((ptr+i*4)+j));
printf("\n");
}
return 0;
}
Example 4: two dimensional array in java example program
String[][][] data = new String[3][4][2];
Example 5: Java 2d array initialization
class TwoDimensionalArray {
public static void main(String[] args) {
String[][] salutation = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Kumar"}
};
System.out.println(salutation[0][0] + salutation[1][0]);
System.out.println(salutation[0][1] + salutation[1][0]);
}
}
The output from this program is:
Mr. Kumar
Mrs. Kumar
Example 6: initialize 2d array c++
#include <array>
2 #include <iostream>
3
4 using namespace std;
5
6
7 const int ROWS = 2;
8 const int COLS = 3;
int main(){
array<array<int, COLS>, ROWS> matrix = {
1, 2, 3,
4, 5, 6
};
return 0;
}