Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/** * Returns a NEW two-dimensional array where each row represents the * result

ID: 3637952 • Letter: #

Question

/**
* Returns a NEW two-dimensional array where each row represents the
* result of appending row values from the second array to the first one.
* For example:
* <pre>
* double[][] first = {{10, 4}, {6, 14}};
* double[][] second = {{20}, {34, 100, 200}};
* double[][] results = Utilities.appendRows(first, second);
* </pre>
* results will corresponds to the two-dimensional array:<br />
* {{10, 4, 20}, {6, 14, 34, 100, 200}}
*
* @param first two-dimensional array of double values or null
* @param second two-dimensional array of double values or null
* @return two-dimensional array of double values that combines row values from
* the original arrays. The method will return null if first and second have
* different number of rows. The method will also return null if first or
* second (or both) are null.
*/

Explanation / Answer

Hi, I'm assuming you need a method implemented in java, the appendRows method. Although you could have made the question clearer by explicitly writing that :)

I took the liberty of putting it in a Utilities class and setting it as a static method so that you can run the example you have in the decription from a Main method and get that result.

I added plenty of comments to make the code clearer. If something is not right, missing, or just not clear. Send me a PM and we can look at it. But I think you should be OK with this, just make sure you trace the code and understand it well rather than just copy-pasting as is.

I hope it helps, please remember to rate :)

public class Utilities {

    public static double[][] appendRows(double[][] first, double[][] second) {
        /* if first and second have different row lengths - return null */
        if (first.length != second.length) {
            return null;
        }

        /* if first or second or both null - return null */
        if ((first == null) || (second == null)) {
            return null;
        }
        /* initialize the results 2D array with
         * the number of rows is the same as first and second
         * the number of elts from first + second since it gets appended
         * it varies from one row to another according to the elts in it
         * for each of the first and second array
         */
        int rows = first.length;
        double[][] results = new double[rows][];
        for (int i = 0; i < rows; i++) {
            int elts = first[i].length + second[i].length;
            results[i] = new double[elts];
        }

        /* write the content of first into results  */
        for (int i = 0; i < first.length; i++) {
            for (int j = 0; j < first[i].length; j++) {
                results[i][j] = first[i][j];
            }
        }

        /* append the content of second where first left off on each row */
        for (int i = 0; i < second.length; i++) {
            for (int j = 0; j < second[i].length; j++) {
                results[i][j + first[i].length] = second[i][j];
            }
        }

        return results;
    } /* end of appendRows method*/
}/* end of Utilities class */