Array
- Is a data type having same type of variables referred by common name.
- elements can access by its index.
- having single or more than one dimensional.
one-dimensional array
syntax:
type var_name; // declaration
var_name= new type[size]; // allocates memory by using new keyword.
var_name[index]=value; // initializes the one value to variable in array.
ex:
int a[]; //declaration
a=new int[10];// allocates memory for 10 elements
a[0] = 1; // initializes first element with value 1.
int a[]=new int[10]; // declaration and allocation in one statement.
int a[]={1,2,3,4,5};//array initialization, no need of new keyword.
java makes sure that the number of elements in array should not be greater than its size.
ex:
0 to 9 only.
// example for single dimensional array
class SingleDimensionalArrayExample
{
public static void main(String args[])
{
int a[]=new int [10];
int k=1;
for(int i=0;i<10;i++)
a[i]=k++;
System.out.println("elements in array are::");
for(int j=0;j<10;j++)
System.out.print(a[j]+" ");
}
}
Output::
elements in array are::
1 2 3 4 5 6 7 8 9 10
Multidimensional Arrays
It is an array of arrays.
int two__dimensional__array[][] = new int[3][4]; // declaration and allocation of twoD array. arrays of array int.
or
int two__dimensional__array[][] = new int[3][];
two__dimensional__array[0]= new int[4];
two__dimensional__array[1]= new int[4];
two__dimensional__array[2]= new int[4];
//example for two-dimensional-array
class TwoDimensionalArrayExample1
{
public static void main(String args[])
{
int a[][]=new int[3][4];
int i,j,k=1;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
a[i][j]=k++;
System.out.println("elements in array are::");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
output::
elements in array are::
1 2 3 4
5 6 7 8
9 10 11 12
we can use different sizes in second dimension.
//example for using different sizes in second dimension.
class TwoDimensionalArrayExample2
{
public static void main(String args[])
{
int a[][]=new int[3][];
a[0]=new int[3];
a[1]=new int[2];
a[2]=new int[1];
int i,k=1;
for(i=0;i<3;i++)
for(int z=0,j=3;j>i;j--)
a[i][z++]=k++;
System.out.println("elements in array are::");
for(i=0;i<3;i++)
{
for(int z=0,j=3;j>i;j--)
System.out.print(a[i][z++]+" ");
System.out.println();
}
}
}
Output::
elements in array are::
1 2 3
4 5
6
we can use expressions in the initialization of array
//example of using expressions in the initialization of array
class TwoDimensionalArrayExample3
{
public static void main(String args[])
{
int a[][]={{1*1},{1+1,22%5},{1*3,6-3,21/7}};
System.out.println("elements in array are::");
for(int i=0;i<3;i++)
{
for(int j=0;j<i+1;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
}
output::
1
2 2
3 3 3
example for three dimensional array
//example for three dimensional array
class ThreeDimensionalArrayExample
{
public static void main(String args[])
{
int a[][][]=new int[2][2][3];
int i,j,k;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<3;k++)
a[i][j][k]=i+j+k;
System.out.println("elements in array are::");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<3;k++)
System.out.print(a[i][j][k]+" ");
System.out.println();
}
System.out.println();
}
}
}
Output::
0 1 2
1 2 3
1 2 3
2 3 4