Java Reference
In-Depth Information
Array Type Names
Whenever you need an array type name, whether for the type of an array variable declaration,
the type of an array parameter, or the type for a method that returns an array, you specify
the type name in the same way.
SYNTAX
Base_Type
[]
EXAMPLES
double
[] a =
new double
[10];
int
[] giveIntArray(
char
[] arrayParameter)
{ ... }
Self-Test Exercises
11. Give the defi nition of a method called
halfArray
that has a single parameter
for an array of base type
double
and that returns another array of base type
double
that has the same length and in which each element has been divided
by
2.0
. Make it a static method. To test it, you can add it to any class or, better
yet, write a class with a test program in the method
main
.
12. What is wrong with the following method defi nition? It is an alternate
defi nition of the method by the same name defi ned in the previous subsection.
It will compile.
public static char
[] upperCaseVersion(
char
[] a)
{
char
i;
for
(i = 0; i < a.length; i++)
a[i] = Character.toUpperCase(a[i]);
return
a;
}
Never trust to general impressions, my boy,
but concentrate yourself upon details.
SIR ARTHUR CONAN DOYLE,
A Case of Identity
(SHERLOCK HOLMES)
In this section, we discuss partially filled arrays as well as how to use arrays as class
instance variables.