CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Arrays

Arrays
The Arrays class
Reference to an array

How to refer to arrays

  • To create a reference to an existing array, code an assignment statement that assigns an array variable to the existing array. Then, two variables will point to the same array in memory.

How to create a reference to an array


	Example 1: Code that creates a reference to an array

		double[] grades = {92.3, 88.0, 95.2, 90.5};
		double[] percentages = grades;
		percentages[1] = 70.2;          // changes grades[1] too
		System.out.println("grades[1]=" + grades[1]); // prints 70.2

	Example 2: Code that reuses an array variable

		double[] grades = new grades[5];
		grades = new grades[20]
Previous | Methods | Code | Comparable interface | Reference to an array | Copy | Next