<< 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.

Returning an array from a method

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[] .

  • Initially, the variable 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 .
  • A reference to the array is passed as a parameter to the method reverse . The formal parameter a contains a reference to the same array pointeed to by the actual parameter fib .
  • A new array b of the same type and length as the parameter a is declared and allocated.
  • Each iteration of the for-loop moves one element from the first half of 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.
  • The reference to array b is returned. Although array referenced by b was allocated within the method call, it still exists after returning.
  • The reference that is returned is assigned to reversedFib .

Exercise The program has a bug. Fix it!

Array assignment can create garbage

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.

Questions & Answers

a perfect square v²+2v+_
Dearan Reply
kkk nice
Abdirahman Reply
algebra 2 Inequalities:If equation 2 = 0 it is an open set?
Kim Reply
or infinite solutions?
Embra Reply
if |A| not equal to 0 and order of A is n prove that adj (adj A = |A|
Nancy Reply
rolling four fair dice and getting an even number an all four dice
ramon Reply
Kristine 2*2*2=8
Bridget Reply
Differences Between Laspeyres and Paasche Indices
Emedobi Reply
No. 7x -4y is simplified from 4x + (3y + 3x) -7y
Mary Reply
is it 3×y ?
Joan Reply
J, combine like terms 7x-4y
Bridget Reply
im not good at math so would this help me
Rachael Reply
how did I we'll learn this
Noor Reply
f(x)= 2|x+5| find f(-6)
Prince Reply
f(n)= 2n + 1
Samantha Reply
Need to simplify the expresin. 3/7 (x+y)-1/7 (x-1)=
Crystal Reply
. After 3 months on a diet, Lisa had lost 12% of her original weight. She lost 21 pounds. What was Lisa's original weight?
Chris Reply
preparation of nanomaterial
Victor Reply
Yes, Nanotechnology has a very fast field of applications and their is always something new to do with it...
Himanshu Reply
can nanotechnology change the direction of the face of the world
Prasenjit Reply
At high concentrations (>0.01 M), the relation between absorptivity coefficient and absorbance is no longer linear. This is due to the electrostatic interactions between the quantum dots in close proximity. If the concentration of the solution is high, another effect that is seen is the scattering of light from the large number of quantum dots. This assumption only works at low concentrations of the analyte. Presence of stray light.
Ali Reply
the Beer law works very well for dilute solutions but fails for very high concentrations. why?
bamidele Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Learning objects for java (with jeliot). OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10915/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?