Tino APCS

Arrays as parameters

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

The main method declares an array named data. The array is initialized with the values 0...5 inside the main method.

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.

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.

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.

Dark Mode

Outline