public class MultidimensionalArraysExercises02{
Simulates data with 0.000 in the first element and 0.001 in the second, etc.
@param array
public static void populate( double[][] array ){
double value = 0.0;
double step = 0.001;
for( int i = 0; i < array.length; ++i){
for( int j = 0; j < array[i].length; ++j){
array[i][j] = value;
value += step;
}
}
}
public static void populate2( double[][] array ){
double value = 0.0;
double step = 0.001;
for( int i = 0; i < array.length; ++i){
double[] monthArray = array[i];
for( int j = 0; j < monthArray.length; ++j){
monthArray[j] = value;
value += step;
}
}
}
Calculates the total of all of the values
@param array
@return
public static double total( double[][] array ){
double total = 0.0;
for( int i = 0; i < array.length; ++i){
for( int j = 0; j < array[i].length; ++j){
total += array[i][j];
}
}
return total;
}
public static void main( String[] args ){
double[][] rainfallRectangular = new double[12][31];
double[][] rainfall = new double[12][];
int monthIndex = 0;
rainfall[ monthIndex++ ] = new double[31];
rainfall[ monthIndex++ ] = new double[28];
rainfall[ monthIndex++ ] = new double[31];
rainfall[ monthIndex++ ] = new double[30];
rainfall[ monthIndex++ ] = new double[31];
rainfall[ monthIndex++ ] = new double[30];
rainfall[ monthIndex++ ] = new double[31];
rainfall[ monthIndex++ ] = new double[31];
rainfall[ monthIndex++ ] = new double[30];
rainfall[ monthIndex++ ] = new double[31];
rainfall[ monthIndex++ ] = new double[30];
rainfall[ monthIndex++ ] = new double[31];
populate( rainfall );
System.out.println( "Last rainfall amount: " + rainfall[ 11 ][ 30 ] );
double totalRainfall = total( rainfall );
System.out.println( "Total rainfall: " + totalRainfall );
double[][][] rainfallCentury = new double[100][12][31];
double[][] rainfallRagged = new double[100][12][];
for( int yearI = 0; yearI < rainfallRagged.length; ++yearI){
int monthIndex = 0;
rainfallRagged[yearI][ monthIndex++ ] = new double[31];
rainfallRagged[yearI][ monthIndex++ ] = new double[28];
rainfallRagged[yearI][ monthIndex++ ] = new double[31];
rainfallRagged[yearI][ monthIndex++ ] = new double[30];
rainfallRagged[yearI][ monthIndex++ ] = new double[31];
rainfallRagged[yearI][ monthIndex++ ] = new double[30];
rainfallRagged[yearI][ monthIndex++ ] = new double[31];
rainfallRagged[yearI][ monthIndex++ ] = new double[31];
rainfallRagged[yearI][ monthIndex++ ] = new double[30];
rainfallRagged[yearI][ monthIndex++ ] = new double[31];
rainfallRagged[yearI][ monthIndex++ ] = new double[30];
rainfallRagged[yearI][ monthIndex++ ] = new double[31];
}
for( int yearI = 0; yearI < rainfallRagged.length; ++yearI){
populate( rainfallRagged[yearI] );
}
}
}