Java Reference
In-Depth Information
int
[] z= MultiplyMatrixVector (M,v) ;
for
(
int
i=0;i
<
z . length ; i++)
System . out . print ( z [ i ]+
""
);
}
}
Thus it is quite easy to write basic functions of
linear algebra
. Note that in Java,
it is not necessary
6
to provide the function with the array dimensions since we
can retrieve these dimensions with the
length
keyword, as shown below:
Program 4.12
Creating multidimensional arrays and retrieving their
dimensions
class
MultidimArrays
static void
f2D(
double
[] [] tab)
{
System . out . println (
"Number of lines:"
+tab . l e n g t h ) ;
System . out . println (
"Number of columns:"
+tab[0]. length);
static void
f3D(
double
[] [] [] tab)
{
System . out . println (
"Number of lines X:"
+tab . l e n g t h ) ;
System . out . println (
"Number of columns Y:"
+tab [0]. length) ;
System . out . println (
"Number of depths Z:"
+tab[0][0]. length);
}
public static void
main( String [ ]
args )
{
double
[]
[] var=
new double
[3][4];
f2D( var ) ;
double
[]
[]
[] tmp=
new double
[4][5][7];
f3D(tmp) ;
}
}
Running this program, we see that we correctly retrieved the 2D and 3D array
dimensions given as function arguments:
Number of lines:3
Number of columns:4
Number of lines X:4
Number of columns Y:5
Number of depths Z:7
4.5.2 Multi-dimensional ragged arrays **
Multi-dimensional arrays need not be regular: They can be completely irregular.
That is, a
multi-dimensional array can also be defined as a 1D array of arrays,
6
In the C programming language, one
has
to pass these dimensions as arguments.
Search WWH ::
Custom Search