Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用两种方法添加行和列_Java_Arrays - Fatal编程技术网

Java 使用两种方法添加行和列

Java 使用两种方法添加行和列,java,arrays,Java,Arrays,请帮助,我应该使用两个单独的方法(一个用于列,一个用于行)来汇总2d数组的每一行和每一列,并将它们打印出来(例如:行1=,行2=,列1=,列2=)。到目前为止,我有两个单独的方法,分别获得第一行和第列,但我一直在研究如何在不更改返回值的情况下打印其他行/列。以下是我目前掌握的情况: public class FinalSumRowColumn { public static void main(String[] args) { int[][] mat = {

请帮助,我应该使用两个单独的方法(一个用于列,一个用于行)来汇总2d数组的每一行和每一列,并将它们打印出来(例如:行1=,行2=,列1=,列2=)。到目前为止,我有两个单独的方法,分别获得第一行和第列,但我一直在研究如何在不更改返回值的情况下打印其他行/列。以下是我目前掌握的情况:

public class FinalSumRowColumn
{
    public static void main(String[] args)
    {

        int[][] mat = {
                        { 1, 2, 3 },
                        { 4, 5, 6 },
                        { 7, 8, 9 }
                      };

        System.out.println("\nSum Row 1 = " + sumRow(mat));  
        System.out.println("\nSum Col 1 = " + sumCol(mat));
    }

    public static int sumRow(int[][] mat)
    {
        int total = 0;

            for (int column = 0; column < mat[0].length; column++)
            {
                total += mat[0][column];
            }
        return total;
    }

    public static int sumCol(int[][] mat)
    {
        int total = 0;

            for (int row = 0; row < mat[0].length; row++)
            {
                total += mat[row][0];
            }
        return total;
    }
}
公共类FinalSumRowColumn
{
公共静态void main(字符串[]args)
{
int[][]mat={
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
System.out.println(“\nSum Row 1=“+sumRow(mat));
System.out.println(“\nSum Col 1=“+sumCol(mat));
}
公共静态int sumRow(int[]mat)
{
int-total=0;
对于(int column=0;column
为什么不在两个方法中都添加一个参数,以指示要求和的行或列的索引


例如,
公共静态int-sumRow(int[][]mat,int-row)

将方法定义更改为:

public static int sumRow(int[][] mat)
致:

然后对传递给方法的行求和:

total += mat[row][column];

对于
sumCol()

也一样,为这些方法添加
col
参数,例如:

public class FinalSumRowColumn {
    public static void main(String[] args) {

        int[][] mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

        System.out.println("\nSum Row 1 = " + sumRow(mat, 0));
        System.out.println("\nSum Col 1 = " + sumCol(mat, 0));
        System.out.println("\nSum Row 1 = " + sumRow(mat, 1));
        System.out.println("\nSum Col 1 = " + sumCol(mat, 1));
        System.out.println("\nSum Row 1 = " + sumRow(mat, 2));
        System.out.println("\nSum Col 1 = " + sumCol(mat, 2));
    }

    public static int sumRow(int[][] mat, int row) {
        int total = 0;

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

    public static int sumCol(int[][] mat, int col) {
        int total = 0;

        for (int row = 0; row < mat[0].length; row++) {
            total += mat[row][col];
        }
        return total;
    }
}
public static int sumRow(int[][] mat, int index)
{
    int total = 0;

    for (int column = 0; column < mat[index].length; column++)
    {
        total += mat[index][column];
    }
    return total;
}
公共类FinalSumRowColumn{
公共静态void main(字符串[]args){
int[]mat={{1,2,3},{4,5,6},{7,8,9};
System.out.println(“\nSum Row 1=“+sumRow(mat,0));
System.out.println(“\nSum Col 1=“+sumCol(mat,0));
System.out.println(“\nSum Row 1=“+sumRow(mat,1));
System.out.println(“\nSum Col 1=“+sumCol(mat,1));
System.out.println(“\nSum Row 1=“+sumRow(mat,2));
System.out.println(“\nSum Col 1=“+sumCol(mat,2));
}
公共静态int sumRow(int[]mat,int row){
int-total=0;
对于(int column=0;column
为每个方法再添加一个参数:
int index
,例如:

public class FinalSumRowColumn {
    public static void main(String[] args) {

        int[][] mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

        System.out.println("\nSum Row 1 = " + sumRow(mat, 0));
        System.out.println("\nSum Col 1 = " + sumCol(mat, 0));
        System.out.println("\nSum Row 1 = " + sumRow(mat, 1));
        System.out.println("\nSum Col 1 = " + sumCol(mat, 1));
        System.out.println("\nSum Row 1 = " + sumRow(mat, 2));
        System.out.println("\nSum Col 1 = " + sumCol(mat, 2));
    }

    public static int sumRow(int[][] mat, int row) {
        int total = 0;

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

    public static int sumCol(int[][] mat, int col) {
        int total = 0;

        for (int row = 0; row < mat[0].length; row++) {
            total += mat[row][col];
        }
        return total;
    }
}
public static int sumRow(int[][] mat, int index)
{
    int total = 0;

    for (int column = 0; column < mat[index].length; column++)
    {
        total += mat[index][column];
    }
    return total;
}
public static int sumRow(int[][]mat,int index)
{
int-total=0;
对于(int column=0;column
打印时:

for (int i = 0; i < mat.length; i++) {
    System.out.println("Sum Row " + (i+1) + " = " + sumRow(mat, i));
}
for(int i=0;i
为什么返回的是
int
而不是包含和的数组?这些方法应该返回特定列/行的和,还是所有列/行的和?啊哈!我忽略了添加参数的主要细节。谢谢,我会试试的。很好,我只是需要更改行/列的编号,否则就太完美了。谢谢你,朋友。