仅对2D数组的行求和并将总和放入新数组中

时间:2016-04-19 12:54:24

标签: java arrays for-loop

我需要修复下面的代码,以获取2D数组行中值的总和,并将它们放在一个新数组中。我尝试了很多不同的方法,但似乎无法弄清楚如何做到正确。

问题在于hoursArray方法:

//Bronson Lane 4/18
//This program computes employee's weekly hours and sorts them in descending order
package hw12;
import java.util.*;

public class HW12 {
    public static double[][] hoursArray(double[][] weeklyHours) {
        Scanner input = new Scanner(System.in);
        double[][] total = new double[weeklyHours.length][];
        //double sum[][] = new double[weeklyHours.length][];

        for (int row = 0; row < weeklyHours.length; row++) {
            total = total[row][];
            for (int column = 0; column < weeklyHours[row].length; column++) {
                total = total + weeklyHours[row][column];

            }
        }
        return total;
    }

    public static void selectionSort(double[][] list) {

    }

    public static void main(String[] args) {
        double[][] weeklyHours = {{2, 4, 3, 4, 5, 8, 8},
                {7, 3, 4, 3, 3, 4, 4},
                {3, 3, 4, 3, 3, 2, 2},
                {9, 3, 4, 7, 3, 4, 1},
                {3, 5, 4, 3, 6, 3, 8},
                {3, 4, 4, 6, 3, 4, 4},
                {3, 7, 4, 8, 3, 8, 4},
                {6, 3, 5, 9, 2, 7, 9}};
        double cool = hoursArray(weeklyHours);

        System.out.println(cool);
    }
}

2 个答案:

答案 0 :(得分:1)

好的,让我们仔细考虑你的问题,你希望你的函数得到一个二维数组并输出一个数组,所以首先你的函数hoursArray应该返回double[]而不是double[][]

现在输出数组应该有多大?如果2d数组的大小为n * m,那么输出数组应该有n个元素,因为它是我们想要求和的行,所以我们将总数实例化为total=new double[weeklyhours.length]

现在我们循环weeklyhours的每一行并总结它们,但是如何?

请记住,2d数组实际上是数组的数组,每一行都是一个数组,如果你知道如何对数组求和,那么这是相同的,不同之处在于我们将总和放在一起。

在1d数组中,我们在for循环中写sum+= a[i],这里我们写sum[row]+=a[row][i]i几乎相同,区别在于row的加法。

因为我们想为每一行做1d数组的总和,所以我们有两个for循环:一个循环行,另一个循环行的数组元素。

当外部for循环结束时,我们返回sum这是一个1d数组,正是我们打算做的。

将所有这些部分放在一起,我们以此代码结束:

static double[] hoursArray(double[][] weeklyhours)
{
    double[] sum=new double[weeklyhours.length]
    for(int row=0;row < sum.length;row++)
    {
        for(int i=0;i<weeklyhours[row].length;i++)
            sum[row]+=weeklyhours[row][i]
    }
    return sum;
}

注意:

你的函数应该接受输入并产生输出,输入来自哪里必须在main或你决定处理输入的任何函数中决定,hoursArray不应该读{{1}用户将其作为参数获取时。

答案 1 :(得分:1)

我在你的代码测试中进行了一些测试

public static double[] hoursArray(double[][] weeklyHours) {
    Scanner input = new Scanner(System.in);
    double[] total = new double[weeklyHours.length];
    //double sum[][] = new double[weeklyHours.length][];

    for (int row = 0; row < weeklyHours.length; row++) {
        double sum = 0;
        for (int column = 0; column < weeklyHours[row].length; column++) {
            sum += weeklyHours[row][column];
        }
        total[row] = sum;
    }
    return total;
}

public static void selectionSort(double[][] list) {

}

public static void main(String[] args) {
    double[][] weeklyHours = {{2, 4, 3, 4, 5, 8, 8},
            {7, 3, 4, 3, 3, 4, 4},
            {3, 3, 4, 3, 3, 2, 2},
            {9, 3, 4, 7, 3, 4, 1},
            {3, 5, 4, 3, 6, 3, 8},
            {3, 4, 4, 6, 3, 4, 4},
            {3, 7, 4, 8, 3, 8, 4},
            {6, 3, 5, 9, 2, 7, 9}};

    double cool[] = hoursArray(weeklyHours);
    for(double co : cool){
        System.out.print(co + ",");
    }
}