阅前声明: http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx
/****************** Exercise 19 *****************
* Write a method that creates and initializes a
* two-dimensional array of double. The size of
* the array is determined by the arguments of
* the method, and the initialization values are
* a range determined by beginning and ending
* values that are also arguments of the method.
* Create a second method that will print the
* array generated by the first method. In main()
* test the methods by creating and printing
* several different sizes of arrays.
***********************************************/
public class E19_TwoDDoubleArray {
public static double[][] twoDDoubleArray(
int xLen, int yLen,
double valStart, double valEnd){
double[][] array = new double[xLen][yLen];
double increment =
(valEnd - valStart)/(xLen * yLen);
double val = valStart;
for(int i = 0; i < array.length; i++)
for(int j = 0; j < array[i].length; j++) {
array[i][j] = val;
val += increment;
}
return array;
}
public static void printArray(double[][] array){
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++)
System.out.print(" " + array[i][j]);
System.out.println();
}
}
public static void main(String args[]) {
double[][] twoD =
twoDDoubleArray(4, 6, 47.0, 99.0);
printArray(twoD);
System.out.println("**********************");
double[][] twoD2 =
twoDDoubleArray(2, 2, 47.0, 99.0);
printArray(twoD2);
System.out.println("**********************");
double[][] twoD3 =
twoDDoubleArray(9, 5, 47.0, 99.0);
printArray(twoD3);
}
}
//+M java E19_TwoDDoubleArray
**The step value for the initialization range is calculated by dividing the range by the number of elements in the array.