Arrays


Java and Fortran represent and treat arrays in different ways. Fortran arrays are defined using the dimension keyword followed by the array name and array size in parentheses. Commas separate the dimension sizes of multidimensional arrays. For example, to create a 59x59 array named data , you could type

 REAL DIMENSION(59,59):: data 

Java arrays are reference types. They are created using the new keyword. You must specify the type of data stored in the array. The size of the array is specified using square brackets. One set of brackets is used for every dimension. For example, to create a 59x59 Java array named data that would contain elements of data type double , you could type

 double data[][] = new double[59][59]; 

There are other array differences as well. Fortran array subscripts start from the number 1 by default but the range can be specified explicitly. Java array indices start from the number 0 and the range cannot be customized. For tran array elements can be accessed by placing the desired subscript inside parentheses after the array name. For example, to assign the value 4.0 to the element in the first row and first column of the previously declared data array, you would type

 data(1,1) = 4.0 

You access an element of a Java array using a pair of square brackets ([]) around the index of the element to be accessed. You use a different pair of square brackets for every dimension. For example, to assign the value 4.0 to the element in the first row and first column of the Java data array, you would type

 data[0][0] = 4.0; 

Java always performs bounds checking for array access. Fortran does not bounds check by default although most compilers support this feature as an option.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net