I should create a method that print the sum of a given column in the main. This program shows a compilation error of:
Error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at algproo2.ALGPROO2.sumColumn(ALGPROO2.java:29)
at algproo2.ALGPROO2.main(ALGPROO2.java:24)
Java Result: 1
What should i do?
public class ALGPROO2
{
public static void main(String[] args)
{
int[][] a = {
{-5,-2,-3,7},
{1,-5,-2,2},
{1,-2,3,-4}
};
System.out.println(sumColumn(a,1)); //should print -9
System.out.println(sumColumn(a,3)); //should print 5
}
public static int sumColumn(int[][] array, int column)
{
int sumn = 0;
int coluna [] = array[column];
for (int value : coluna )
{
sumn += value;
}
return sumn;
}
}