252 Arrays Chapter 7 54 55 // method (Starting a web site)
252 Arrays Chapter 7 54 55 // method modifies the array it receives, 56 // original will be modified 57 public void ModifyArray( int[] b ) 58 { 59 for ( int j = 0; j < b.Length; j++ ) 60 b[ j ] *= 2; 61 } 62 63 // method modifies the integer passed to it 64 // original will not be modified 65 public void ModifyElement( int e ) 66 { 67 outputLabel.Text += 68 "nvalue received in ModifyElement: " + e; 69 70 e *= 2; 71 72 outputLabel.Text += 73 "nvalue calculated in ModifyElement: " + e; 74 } 75 } Fig. 7.8 Passing arrays and individual array elements to methods. (Part 2 of 2.) To show the value of a[3] before the call to ModifyElement, lines 44 46 append the value of a[3] (and other information) to outputLabel.Text. Line 44 invokes method ModifyElement and passes a[3]. Remember that a[3] is a single int value in the array a. Also, remember that values of primitive types always are passed to methods by value. Therefore, a copy of a[3] is passed. Method ModifyElementmultiplies its argument by 2 and stores the result in its parameter e. The parameter of ModifyElement is a local variable, so when the method terminates, the local variable is destroyed. Thus, when control is returned to PassArray, the unmodified value of a[3] is appended to the outputLabel.Text (line 51 52).
Check Tomcat Web Hosting services for best quality webspace to host your web application.