Arrays are used to put similar types of multiple variables.
Arrays can hold similar types of objects or primitive types.
Array variables are always considered as object in java.
During the construction of an Array object we need to mention the size of that object.
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 60;
a[5] = 70;
obj[0] = new Object();
obj[1] = new Object();
2 D Array
a2[0] = new int[3];
a2[1] = new int[4];
a2[2] = new int[5];
a2[3] = new int[6];
a2[0][1] = 10;
a2[1][3] = 20;
a2[2][1] = 30;
a2[2][4] = 50;
a2[3][4] = 40;
Arrays can hold similar types of objects or primitive types.
Array variables are always considered as object in java.
Declaration of Array
int[] a; int a[]; Object[ ] obj; Object obj[]; // square bracket or after the variable both are recommended. //2 -d array int [][] a2; int a2 [][]; Object[ ][] obj; Object obj[][] ;
Instantiating or Constructing an Array
Declaration of an array does not mean a heap space is allocated for that array.Heap space is allocated when an array is instantiated or created.During the construction of an Array object we need to mention the size of that object.
a = new int[6]; obj = new Object[10]; // 2-d array a2 = new int[4][5]; a3 = new int[4][]; obj2 = new Object[3][4];
Initializing an Array
We have already declared and initialized an Array now we need to learn how to put values on the initialized array.a[0] = 10;
a[1] = 20;
a[2] = 30;
a[3] = 40;
a[4] = 60;
a[5] = 70;
obj[0] = new Object();
obj[1] = new Object();
2 D Array
a2[0] = new int[3];
a2[1] = new int[4];
a2[2] = new int[5];
a2[3] = new int[6];
a2[0][1] = 10;
a2[1][3] = 20;
a2[2][1] = 30;
a2[2][4] = 50;
a2[3][4] = 40;
No comments:
Post a Comment