Variable size arrays in java

I have created a variable size array in java.Now i want to sum all the elements vertically columnwise.
How should i proceed?

1 Like

You may proceed in the following way :
In java a multidimensional array is simply an array of arrays. So you can get the number of rows by using the following line in your code.
Let the name of your multidimensional array be ‘a’, then,

int rows = a.length;

now for getting number of columns you can write,

int columns = a[0].length;

After getting the number of rows and columns you can simply use a loop (while, for etc.) for calculating the sum vertically. Take an array with size equal to the number of columns and iterate the multidimensional array row by row simultaneously storing the sum in your new array. Your code may look like this :

**for(int i=0; i< rows; i++)
{
   for(int j=0; j< columns;j++)

{

s[j]+=a[i][j];

}

}**

//