Java Reference
In-Depth Information
Since an indexed variable of the array a is also a variable of type double , just like n , the
following is equally legal:
myMethod(a[3]);
There is one subtlety that does apply to indexed variables used as arguments. For
example, consider the following method call:
myMethod(a[i]);
If the value of i is 3 , then the argument is a[3] . On the other hand, if the value of i is 0 ,
then this call is equivalent to the following:
myMethod(a[0]);
The indexed expression is evaluated to determine exactly which indexed variable is
given as the argument.
Array Indexed Variables as Arguments
An array indexed variable can be used as an argument anyplace that a variable of the array's
base type can be used. For example, suppose you have the following:
double [] a = new double [10];
Indexed variables such as a[3] and a[index] can then be used as arguments to any
method that accepts a double as an argument.
entire array
parameters
You can also define a method that has a formal parameter for an entire array so that
when the method is called, the argument that is plugged in for this formal parameter
is an entire array. Whenever you need to specify an array type, the type name has the
form Base_Type [] , so this is how you specify a parameter type for an entire array. For
example, the method doubleArrayElements , given in what follows, will accept any
array of double as its single argument:
public class SampleClass
{
public static void doubleArrayElements( double [] a)
{
int i;
for (i = 0; i < a.length; i++)
a[i] = a[i]*2;
}
< The rest of the class definition goes here.>
}
 
Search WWH ::




Custom Search