There are two type that is associated with memory allocation of array.First one is static and another one is dynamic allocation.The size of array is predefined in static allocation where as the size is unknown in dynamic allocation.In dynamic allocation size of it index is decided at run time.So it is better operation when you are working with it by considering memory management problem.
The wastage of memory is less in matter of dynamic allocation.The syntax is same in previous array tutorial .Generally when we are working with patterns example, this two types are used to assign  a method of allocation.If you think that it is same as c/c++ ,then you are wrong! In c/c++ we can not give null size while defining array of variable.We have to define it’s size.So it’s better that we have facility to decide size of array during run time in java.








There are two different example of static and dynamic allocation from which you will get better knowledge about it’s allocation

Click here for more array guide

Static memory allocation in array

Problem–>>  Print the below pattern with using static memory allocation in array

Code–>>

class pattern2{
public static void main(String args[])
{
int pym[][]=new int[4][4];
int i,j,k=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
pym[i][j]=k;
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(pym[i][j]);
}
System.out.println(“”);
}
}
}

Output–>>

0000
0000
0000
0000

Logic–>>
First we have defined array variable having two dimensions and simultaneously we have initialized it’s memory size with using static allocation.Here we want four rows and columns .So there is no need of dynamic allocation because the number of rows and column is fixed.We want 0(zero) in every row and column index. so we have taken a variable named ‘k’ and initialized it’s value 0(zero).Now we have used to ‘for’ loop statement ,one for row and another for colum.In every index of array we have initialized it to zero.We have used same two for statements for printing its row and column.


Dynamic memory allocation in array

Problem–>> Print this pattern with using dynamic memory allocation in array

Code–>>

class pattern1{
public static void main(String args[])
{
int pym[][]=new int[4][];
pym[0]=new int[1];
pym[1]=new int[2];
pym[2]=new int[3];
pym[3]=new int[4];
int i,j,k=0;
for(i=0;i<4;i++)
{
for(j=0;j<i+1;j++)
{
pym[i][j]=k;
k++;
}
}
for(i=0;i<4;i++)
{
for(j=0;j<i+1;j++)
{
System.out.print(pym[i][j]);
}
System.out.println(” “);
}
}
}

Output–>>



0
12
345
6789

Logic–>>

Here we have one fixed allocation for row.But for every row, number of coloumns are differ.So we have to use dynamic memory allocation for columns.So we have defined array of two dimensions and initialized its row size but coloumn size is null .We can see that for first row there is one column,for second row there are two columns ,for third there are three columns and for fourth row it have four columns.So we have initialized all column’s values for particular row’s index,We can also do it with using for loop in small block of code.After that we have initialized k with zero. In second for loop statement we have a condition like j<i+1.We used his condition because for first row it’s index is zero and column is one,For second row index is one and there are two columns..so on. Inside second loop we have initialized its all row and column with k and then we have increased value of k.At last we did same process by using same for loop statement as above.

If you still have doubt about it ,contact us.Dont forget to like our facebook fan page.Happy coding!!

By jigar

Leave a Reply

Your email address will not be published. Required fields are marked *