<< Chapter < Page | Chapter >> Page > |
Exercise Instead of declaring the variable
j
outside the for-loop,
declare it just inside the for-loop as follows:
int j = a.length-i-1;
Trace the execution and explain what happens.
Concept An array can be allocated within a method. Although the variable containing the reference to the array is local to the method, the arrayitself is global and the reference can be returned from the method.
Program: Array04.java
// Learning Object Array04
// returning an array from a methodpublic class Array04 {
static int[] reverse(int[] a) { int[] b = new int[a.length];
int j; for (int i = 0; i < a.length / 2; i++) {
j = a.length-i-1; b[j] = a[i];
b[i] = a[j]; }
return b; }
public static void main(/*String[] args*/) {
int[] fib = {0, 1, 1, 2, 3, 5, 8};
int[] reversedFib = reverse(fib);
}}
This program passes an array as a parameter to a method that reverses the elements of the array. The array is reversed into a new array
b
that is allocated in the method
reverse
. It is then returned to the main method and assigned to
reversedFib
, a different variable of the same array type
int[]
.
fib
of type integer array is
allocated. As part of the same statement, the array object is created withits seven fields having the values in the initializer; the reference to
the object is returned and stored in the variable
fib
.reverse
. The formal parameter
a
contains a reference to the same array pointeed to by the actual parameter
fib
.b
of the same type and length as the parameter
a
is declared and allocated.a
to the second half of
b
and one element from the second half of
a
to the first half of
b
. Variables
i
and
j
contain the indices of the two elements that are moved.b
is returned.
Although array referenced by
b
was allocated
within the method call, it still exists after returning.reversedFib
.Exercise The program has a bug. Fix it!
Concept Since an array variable contains a reference to the array itself, if
null
or another value (another array of the same type) is assigned to the
variable, the first array may no longer be accessible. Inaccessible memoryis called
garbage . The Java runtime system includes a
garbage
collector whose task is to return garbage to the pool of memory that can
be allocated.
Program: Array05.java
// Learning Object Array05
// array assignment can create garbagepublic class Array05 {
static int[] first(int[] a) { int[] b = new int[a.length/2];
for (int i = 0; i < a.length / 2; i++)
b[i] = a[i]; return b;
}
public static void main(/*String[] args*/) {
int[] fib = {0, 1, 1, 2, 3, 5, 8};
fib = first(fib); }
}
An array referenced by the variable
fib
is passed as a parameter to a method that moves the values of the elements in the first half of the array
fib
into a new array
b
which is allocated in the method. The new array is returned to the main method and assigned to the variable
fib
, destroying the reference to the original array.
Notification Switch
Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?