Convert 1D Array to 2D Array in Java

Hello Friends in this article i’m going to explain about the program to Convert 1D Array to 2D Array in Java or Convert 1 Dimension Array into 2 Dimension Array in Java . Convert One Dimension Array into Two Dimension Array in Java. Let’s see the program.

Code Explanation :
step 1 : if you debug/run the program then control goes to main method thereafter that go inside of the main.
step 2 : i have taken one integer single dimension array types variable arr and it has elements {11,22,33,44}.
step 3 : next control goes to arr method and it has three parameters.
step 4 : when control goes to arr method compiler search arr method and it starts processing.
step 5 : arr(int[] a,int r1,int c1) . a receive all a values such as {11,22,33,44}. r1=2,c1=2.
step 7 : int [][]two = new int[r1][c1]. int[][] two = new int[2][2]. r1=2,c1=2.
step 8 : for (int y=0;y<r1;y++) for(int y=0; 0<2;0++) for(int x=0;0<2;0++) so condition true and it process inner loop.
step 9 : two[y][x] = a[i]. x=0,y=0,i=0 a[0]=11. two[0][0]=11 next a[1]=22. two[0][1]=22. x=2 condition false.upper for loop continue.
step 10 : two[y][x] = a[i]. x=0,y=1,i=2 a[2]=33. two[x][y]=a[i] two[1][0]=33 next iteration.
step 11 : x=1,y=1,i=3 a[3]=44. two[x][y]=a[i] two[1][1]=44.
step 12 : when x=2,y=2 condition false. loop terminate.

Post Author: adama