// ArrayParameterPassing1.java // Demos difference between parameter passing by-value vs. by reference import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ArrayParameterPassing1 extends JApplet implements ActionListener { // declare and initialize the hard-coded array int a[ ] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }; // GUI Components Container c; JTextArea outputArea; JButton b; public void init( ) { c = getContentPane( ); c.setLayout( new FlowLayout( ) ); outputArea = new JTextArea( 7, 40 ); c.add( outputArea ); b = new JButton( "Pass" ); b.addActionListener( this ); c.add( b ); } public void actionPerformed( ActionEvent e ) { outputArea.append( "Original array:\n" ); // print the array for ( int i = 0; i < a.length; i++ ) outputArea.append( a[ i ] + " " ); // pass the array by-reference modArray( a ); outputArea.append( "\nArray after passing by-reference:\n" ); // print the array for ( int i = 0; i < a.length; i++ ) outputArea.append( a[ i ] + " " ); // pass some of the individual elements by-value for ( int i = 0; i < a.length - 5; i++ ) a[ i ] = modElement( a[ i ] ); outputArea.append( "\nArray after passing some elements by-value:\n" ); // print the array for ( int i = 0; i < a.length; i++ ) outputArea.append( a[ i ] + " " ); } public void modArray( int b[ ] ) { for ( int i = 0; i < b.length; i++ ) b[ i ] *= 3; } public int modElement( int x ) { return x += 100; } }