特定行的Java数组总和 [英] Java array sum of specific row

查看:87
本文介绍了特定行的Java数组总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取第2行的总和.我的代码可以编译,但不输出任何内容.一些指导? 另外,在第一个问题的基础上,我将如何访问第3列中最大的元素?

I am trying to just get the sum of row 2. My code compiles but doesn't print anything. Some guidance? Also, building off of the first question, how would I access the largest element in column 3?

  import java.util.*;

public class TestCode {

    public static void main(String[] args) {
    int array[][] = {{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}};
    int sum = 0;
    //print(array);
    }
    public static void print(int list[][]) {
      for(int row = 0; row < list.length;row++) {
         int[] sum = new int [3];
         for(int column = 0; column < list[row].length; column++) {
            sum[1] = list.length;
         }
         System.out.println(sum);
      }
    }
}

推荐答案

尝试

public class Test {
    public static void main(String[] args) {
        int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
        FindLargestInColumn(array, 2);
        FindSumforRow(array,2);
    }

    public static void FindLargestInColumn(int list[][], int index) {
        int largest = -1;
        for (int[] row : list) {
            if (row[index] > largest)
                largest = row[index];
        }
        System.out.println("The Largest value in column " + (index )+ " is " + largest);
    }
    public static void FindSumforRow(int list[][], int rowIndex) {
        int sum = 0;
        int[] row = list[rowIndex];
        for (int value : row) {
            sum  = sum + value;  
        }
        System.out.println("The Sum of value in row  " + (rowIndex)+ " is " + sum);
    }
}

这篇关于特定行的Java数组总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆