Java 二维数组(参差不齐)-打印每列总和

Java 二维数组(参差不齐)-打印每列总和,java,arrays,for-loop,multidimensional-array,indexoutofboundsexception,Java,Arrays,For Loop,Multidimensional Array,Indexoutofboundsexception,我试图计算2D数组的列式和 对于此二维阵列: int[][] array = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; int[][] array = {{1,2},{5,6,7},{9,10,11,12}}; 我很容易打印每一列的总和 这是我的代码 int total; for (int col = 0; col < array[0].length; col++) { total = 0; for (int row = 0; row < a

我试图计算2D数组的列式和

对于此二维阵列:

int[][] array = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int[][] array = {{1,2},{5,6,7},{9,10,11,12}};
我很容易打印每一列的总和

这是我的代码

int total;
for (int col = 0; col < array[0].length; col++)
{
  total = 0;
  for (int row = 0; row < array.length; row++)
    total += array[row][col];
  System.out.println("Column " + col + " total: " + total);
}
我似乎无法打印最后两列而不出现边界外异常错误。我们的教授并没有真正教给我们尝试和抓住的说法,所以我假设一定有某种小小的调整。然而,我已经篡改了上面的代码来打印最后两列,但是没有运气

有什么想法吗?

试试这个:

int total;
int max = //This is the max number of column one row can have 
for (int col = 0; col < max; col++)
{
  total = 0;
  for (int row = 0; row < array.length; row++)
    if(col < array[row].length)//Check for row length here.
       total += array[row][col];
  System.out.println("Column " + col + " total: " + total);
}
int-total;
int max=//这是一行可以包含的最大列数
用于(int col=0;col
基本上,在访问其元素之前,需要首先检查行的长度

要查找max,请执行以下操作:

int max = 0;
for(int i = 0; i < array.length; i++)
    max = Math.max(array[i].length, max);
int max=0;
for(int i=0;i
您不需要捕获任何异常。首先,您应该找出2D数组中最长的行(您需要一个初步的循环)


假设是x。然后在外部循环中从0迭代到x-1,在内部循环中,在访问
array[row][col]
之前,确保
array[row].length>col
您的教授可能不会接受这一点,因为他还没有教过您这一点,但最优雅的解决方案是让Java自己做低级循环决策。您可以通过以下方式执行此操作:


它的工作方式是指定数组及其元素的类型。因此,
int[][]
是一个
int[]
的数组,这就是为什么要为(int[]行:数组)
指定
。您可以将其理解为“对于此二维
数组中的每个一维
行[]
”。在循环中,您在
行[]
的每个
int
上嵌套另一个循环,下面是程序的正确代码::我从上面的帮助材料中学习了,但我得到了错误,因为代码是以位和片段形式存在的,尤其是最大值被取为最大值(如果我们需要下一个计数器中较低的max数,则此程序失败)

package-arrayPractice;
公共类数组{
公共静态void main(字符串[]args){
int[][]矩阵=新int[5][];
矩阵[0]=新整数[2];
矩阵[1]=新整数[3];
矩阵[2]=新整数[4];
矩阵[3]=新整数[2];
矩阵[4]=新整数[1];
int-total=0;
int max=0;
对于(int row=0;row
有效!谢谢!但是
max
只能是一个固定值?它不能是像array.length这样的值?@user3718591看看我更新的答案,你需要遍历所有行,找到列的最大值:)谢谢你的解释。现在它变得更有意义了。
int total = 0;
int iterator = 0; // this variable is only necessary if you need to know which row you're in
for (int[] row : array) {
    int sum = 0;
    for (int item : row) {
        sum += item;
    }
    System.out.println("Column " + iterator + " total: " + sum);
    total += sum;
    iterator++;
}
System.out.println(total);
package arrayPractice;

public class SumColumArray {
    public static void main (String [] args){
        int [][] matrix = new int [5][];
            matrix[0] = new int[2];
            matrix[1] = new int[3];
            matrix[2] = new int[4];
            matrix[3] = new int[2];
            matrix[4] = new int[1];


    int total = 0;
    int max = 0;
    for(int row = 0; row < matrix.length; row++){
    max = matrix[row].length; // Variable Length of Column Accessing 
    System.out.println(max);
    for(int column = 0; column < max; column++){
    total = 0;
    matrix[row][column] = (int)(Math.random() * 100);
    if(column < matrix[row].length);
    total += matrix[row][column];
    System.out.println("Column " + column + " total: " + total);

    }
     }
   }
}