Skip to main content
ICT
A16 - Single Dimension Arrays
 
Main   Previous Next
 

D. Arrays as Parameters page 6 of 10

  1. The program ArrayOps.java, provides examples of passing arrays as parameters. Notice that the final integer constant MAX = 6 is used to size the array in this program.
See Handout 16.1, Example Program - Arrays as Parameters ArrayOps.java
  1. The main method declares an array named data. The array is initialized with the values 0...5 inside the main method.

  2. The parameters of the squareList and printList methods are references to an array object. Any local reference to array list inside the squareList or printList methods is an alias for the array data inside of the main method. Notice that after the call of squareList, the values stored in array data in the main method have been permanently changed.

  3. When the rotateList method is called, the copy method of the ArrayOps class is invoked and the local array listCopy is created as a copy of the array data in the main method.

  4. The rotateList method rotates the values one cell to the right, with the last value moved to the front of the list. A call to printList is made inside the rotateList method just before leaving the method. After returning to the main method, notice that the array data is unchanged.

 

Main   Previous Next
Contact
 © ICT 2006, All Rights Reserved.