Answer:
=======================================================
//Class header definition
public class DisArray {
//First method with int array as parameter
public static void convert2D(int[] oneD) {
//1. First calculate the number of columns
//a. get the length of the one dimensional array
int arraylength = oneD.length;
//b. find the square root of the length and typecast it into a float
float squareroot = (float) Math.sqrt(arraylength);
//c. round off the result and save in a variable called row
int row = Math.round(squareroot);
//2. Secondly, calculate the number of columns
//a. if the square of the number of rows is greater than or equal to the
//length of the one dimensional array,
//then to minimize padding, the number of
//columns is the same as the number of rows.
//b. otherwise, the number of columns in one more than the
// number of rows
int col = ((row * row) >= arraylength) ? row : row + 1;
//3. Create a 2D int array with the number of rows and cols
int [ ][ ] twoD = new int [row][col];
//4. Place the elements in the one dimensional array into
//the two dimensional array.
//a. First create a variable counter to control the cycle through the one
// dimensional array.
int counter = 0;
//b. Create two for loops to loop through the rows and columns of the
// two dimensional array.
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
//if counter is less then the length of the one dimensional array,
//then copy the element at that position into the two dimensional
//array. And also increment counter by one.
if (counter < oneD.length) {
twoD[i][j] = oneD[counter];
counter++;
}
//Otherwise, just pad the array with zeros
else {
twoD[i][j] = 0;
}
}
}
//You might want to create another pair of loop to print the elements
//in the populated two dimensional array as follows
for (int i = 0; i < twoD.length; i++) {
for (int j = 0; j < twoD[i].length; j++) {
System.out.print(twoD[i][j] + " ");
}
System.out.println("");
}
} //End of first method
//Second method with String array as parameter
public static void convert2D(String[] oneD) {
//1. First calculate the number of columns
//a. get the length of the one dimensional array
int arraylength = oneD.length;
//b. find the square root of the length and typecast it into a float
float squareroot = (float) Math.sqrt(arraylength);
//c. round off the result and save in a variable called row
int row = Math.round(squareroot);
//2. Secondly, calculate the number of columns
//a. if the square of the number of rows is greater than or equal to the length of
//the one dimensional array, then to minimize padding, the number of
//columns is the same as the number of rows.
//b. otherwise, the number of columns in one more than the
//number of rows.
int col = (row * row >= arraylength) ? row : row + 1;
//3. Create a 2D String array with the number of rows and cols
String[][] twoD = new String[row][col];
//4. Place the elements in the one dimensional array into the two
// dimensional array.
//a. First create a variable counter to control the cycle through the one
// dimensional array.
int counter = 0;
//b. Create two for loops to loop through the rows and columns of the
//two dimensional array.
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
//if counter is less then the length of the one dimensional array,
//then copy the element at that position into the two dimensional
//array. And also increment counter by one.
if (counter < oneD.length) {
twoD[i][j] = oneD[counter];
counter++;
}
//Otherwise, just pad the array with null values
else {
twoD[i][j] = null;
}
}
}
//You might want to create another pair of loop to print the elements
//in the populated two dimensional array as follows:
for (int i = 0; i < twoD.length; i++) {
for (int j = 0; j < twoD[i].length; j++) {
System.out.print(twoD[i][j] + " ");
}
System.out.println("");
}
} // End of the second method
//Create the main method
public static void main(String[] args) {
//1. Create an arbitrary one dimensional int array
int[] x = {23, 3, 4, 3, 2, 4, 3, 3, 5, 6, 5, 3, 5, 5, 6, 3};
//2. Create an arbitrary two dimensional String array
String[] names = {"abc", "john", "dow", "doe", "xyz"};
//Call the respective methods
convert2D(x);
System.out.println("");
convert2D(names);
} // End of the main method
} // End of class definition
=========================================================
==========================================================
Sample Output
23 3 4 3
2 4 3 3
5 6 5 3
5 5 6 3
abc john dow
doe xyz null
==========================================================
Step-by-step explanation:
The above code has been written in Java and it contains comments explaining each line of the code. Please go through the comments. The actual executable lines of code are written in bold-face to distinguish them from the comments.