14.8k views
1 vote
Write a class called DisArray with methods to convert a 1-dimensional array to a 2-dimensional array. The methods' name should be convert2D. You should create methods to convert int[] and String[], that can be tested against the following class. Your convert2D methods should choose the closest possible square-ish size for the 2D array. For example, if your input array is [10], its 2D conversion should be [3][4] or [4][3] -- you decide if you want to favor rows over columns. Your method should place the elements of the one-dimensional array into the two-dimensional array in row-major order, and fill the remaining elements with 0 (for integer arrays) or null (for String arrays). The process of filling unused elements with 0 or null is called padding. If the input array's length is a perfect square, e.g., [16], then your output should a square array, i.e., [4][4]. For any other size, your objective is to minimize the number of padded elements. For example, if your input is [10] you should opt for a [3][4] array instead of a [4][4]. The former will have only 2 padded elements; the latter 6.

User Jack Kelly
by
4.0k points

1 Answer

3 votes

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.

User Pushingphotons
by
4.5k points