Arrays - Arrays Recall what we said about reference types:...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Arrays Recall what we said about reference types : Reference type = class type, interface type, or array type Reference variable holds a reference (an address) to objects or arrays. (Or the value null to indicate that no object or array is referenced). References do not hold values directly. They hold pointers/memory addresses. An array is a collection of data items of the same type. Examples Example: We can declare an array to hold 50 exam grades: int[] examScores = new int[50]; This declaration sets aside enough memory for 50 ints.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Reference variable examScores points to this new array of 50 ints. Example : If we want to store and use 100 employee records in our program, it is not practical to use a different variable name to refer to each employee record. Instead, we store the records in an array of Employee objects. Instead of: Employee mike; Employee sam; Employee julie; ... We use an array that contains 100 employee objects: Employee[] empRecords = new Employee[100]; In this Java statement, we have: declared a variable empRecords to be of type Employee[], i.e. an array of Employee objects. We have also set aside memory for 100 Employee objects using the new operator.
Background image of page 2
One-Dimensional Arrays Declaring an Array and Allocating Memory for It 1. Declaring an array without setting aside memory for the elements: Type[] arrayName; Example: char[] charsInMyName; // sets aside 4 bytes to store the memory location of an array // of chars // this variable, charsInMyName, doesn't point to an actual
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/24/2010 for the course C S 305j taught by Professor Staff during the Spring '08 term at University of Texas at Austin.

Page1 / 12

Arrays - Arrays Recall what we said about reference types:...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online