Advertisement
class A
{
public static void main(String... ar)
{
int[][] twoDA1; //Declaration of a 2-D array, which will hold arrays of int values.
A[][] twoDA2; //Declaring a 2-D array holding arrays of object references of the type class A.
}
}
class A
{
public static void main(String... ar)
{
int[][] twoDA1= new int[3][]; //Array is constructed on the heap with a size to hold 3 int arrays.
twoDA1[0]= new int[3]; //Declaring and constructing first array which will hold 3 int values.
twoDA1[1]= new int[3]; //Declaring and constructing second array which will hold 3 int values.
twoDA1[2]= new int[3]; //Declaring and constructing third array which will hold 3 int values.
}
}
int[][] twoDA1= new int [3][3];
Array type | Default value |
---|---|
char | '\u0000' |
byte | 0 |
int | 0 |
float | 0.0 |
double | 0.0 | Object | null |
Advertisement
class A
{
public static void main(String... ar)
{
int[][] twoDA= new int[3][3]; //2-D array holding 3 int arrays(each array holding 3 int values).
twoDA[0][0]=10; //inserting 10 in the first array at its 0th index
twoDA[0][1]=20; //inserting 20 in the first array at its 1st index
twoDA[0][2]=30; //inserting 30 in the first array at its 2nd index
twoDA[1][0]=40; //inserting 40 in the second array at its 0th index
twoDA[1][1]=50; //inserting 50 in the second array at its 1st index
twoDA[1][2]=60; //inserting 60 in the second array at its 2nd index
twoDA[2][0]=70; //inserting 70 in the third array at its 0th index
twoDA[2][1]=80; //inserting 80 in the third array at its 1st index
twoDA[2][2]=90; //inserting 90 in the third array at its 2nd index
}
}
twoDA[0][0]=10;
The reference to the first array is specified by zero in the first bracket of the 2D array. Zero in the second bracket specifies the
0th index in the first array at which we are putting int value 10.twoDA[0][1]=20;
We are putting an int value(20) in the first array at its 1st index.twoDA[0][2]=30;
We are putting an int value(20) in the first array at its 2nd index.twoDA[1][0]=40;
The reference to the second array is specified by one in the first bracket of the 2D array. Zero in the second bracket specifies the 0th index of the second array at which we are putting int value 40.twoDA[1][1]=50;
We are putting an int value(50) in the second array at its 1st index.twoDA[1][2]=60;
We are putting an int value(60) in the second array at its 2nd index.System.out.println(twoDA[1][0]); //Reading the first array and its 0th index
//Creating a 2-D array holding 3 int arrays, with each array holding 3 int values.
class A
{
public static void main(String... ar)
{
int[][] twoDA= new int[3][3]; //2-D array holding 3 int arrays(each array holding 3 int values).
twoDA[0][0]=10; //inserting 10 in the first array at its 0th index
twoDA[0][1]=20; //inserting 20 in the first array at its 1st index
twoDA[0][2]=30; //inserting 30 in the first array at its 2nd index
twoDA[1][0]=40; //inserting 40 in the second array at its 0th index
twoDA[1][1]=50; //inserting 50 in the second array at its 1st index
twoDA[1][2]=60; //inserting 60 in the second array at its 2nd index
twoDA[2][0]=70; //inserting 70 in the third array at its 0th index
twoDA[2][1]=80; //inserting 80 in the third array at its 1st index
twoDA[2][2]=90; //inserting 90 in the third array at its 2nd index
//Reading an array
System.out.println("Length of this array is : " + twoDA.length);
System.out.println("Contents of this array are : ");
for(int i=0; i<twoDA.length; i++)
{
for(int j=0; j<twoDA.length;j++)
{
System.out.println(twoDA[i][j]);
}
}
}
}
Length of this array is : 3
Contents of this array are :
10
20
30
40
50
60
70
80
90
Advertisement
class A
{
public static void main(String... ar)
{
A[][] twoDA= new A[2][2]; //2D array holding 2 arrays(each array holding 2 references to objects of type A).
twoDA[0][0]= new A(); //inserting an object of A in the first array at its 0th index.
twoDA[0][1]= new A(); //inserting ab object of A in the first array at its 1st index.
twoDA[1][0]= new A(); //inserting an object of A in the second array at its 0th index.
twoDA[1][1]= new A(); //inserting an object of A in the second array at its 1st index.
//Getting the total length(total number of arrays in a 2D array).
System.out.println("Number of arrays in this 2-D array : " + twoDA.length);
for(int i=0; i<twoDA.length; i++)
{
for(int j=0; j<twoDA.length;j++)
{
System.out.println(twoDA[i][j]);
}
}
}
}
Number of arrays in this 2-D array : 2
A@70dea4e
A@5c647e05
A@33909752
A@55f96302
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement