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

how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Maciej
Do somebody tell me a best nano engineering book for beginners?
s. Reply
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
Virgil
is Bucky paper clear?
CYNTHIA
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
SUYASH Reply
for screen printed electrodes ?
SUYASH
What is lattice structure?
s. Reply
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
China
Cied
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
Porter
many many of nanotubes
Porter
what is the k.e before it land
Yasmin
what is the function of carbon nanotubes?
Cesar
I'm interested in nanotube
Uday
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
AMJAD
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
good afternoon madam
AMJAD
what is system testing
AMJAD
what is the application of nanotechnology?
Stotaw
In this morden time nanotechnology used in many field . 1-Electronics-manufacturad IC ,RAM,MRAM,solar panel etc 2-Helth and Medical-Nanomedicine,Drug Dilivery for cancer treatment etc 3- Atomobile -MEMS, Coating on car etc. and may other field for details you can check at Google
Azam
anybody can imagine what will be happen after 100 years from now in nano tech world
Prasenjit
after 100 year this will be not nanotechnology maybe this technology name will be change . maybe aftet 100 year . we work on electron lable practically about its properties and behaviour by the different instruments
Azam
name doesn't matter , whatever it will be change... I'm taking about effect on circumstances of the microscopic world
Prasenjit
how hard could it be to apply nanotechnology against viral infections such HIV or Ebola?
Damian
silver nanoparticles could handle the job?
Damian
not now but maybe in future only AgNP maybe any other nanomaterials
Azam
Hello
Uday
I'm interested in Nanotube
Uday
this technology will not going on for the long time , so I'm thinking about femtotechnology 10^-15
Prasenjit
can nanotechnology change the direction of the face of the world
Prasenjit Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Berger describes sociologists as concerned with
Mueller 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?

Ask