10/20/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 22:More on Array OperationsLast time:1. Array lengths and out-of-bounds indexing 2. Copying arrays3. Explicit array initializationToday:1. Arrays as arguments2. Arrays of objects3. Array copying4. Privacy leaksCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Project #4 Due Today! The project is closed You must complete the project by yourself Assistance can only be provided by teaching assistants (TAs) and instructors You must not look at other students' code Start now! Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly, as much of grading is automatedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Arrays As Arguments Arrays = objects Array variables = references Array cells = variables Both can be used as arguments to methods Array cells: passed just like variables Array arguments: passed just like objects Reference to array is passed in If the method expects an array of doubles, an array of doubles of any size can be passed Promotion does not apply. You cannot pass an int arrayCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3Examplepublic static void triple(int a) {a *= 3;}public static void triple(int[] a) {for (int i = 0; i < a.length; i++)a[i] *= 3;}public static void main(String[] args) {int[] a = new int[5];for (int i = 0; i < a.length; i++)a[i] = 5;triple(a[0]);System.out.println("After int triple, a[0] is " + a[0]);triple(a);System.out.println("After array triple, a[0] is " + a[0]);}HeapStacka0 0 0 0 05a 55 55515a15 15 15 15 15Output:515CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Arrays of Objects Class types can also be base types of arrays e.g.String[] acc = new String[3]; Array cells store references to objects Array initializers can also be usedString[] acc = {“UMD”, “UNC”, “Duke”}; Expressions can also appear in initializersDate[] midterms = {new Date (12,10,2006),new Date(15,11,2006)};HeapStackaccUMDUNCDukeCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5Three Ways of Copying Arrays of ObjectsDate[] d = {new Date (12,10,2006), new Date (15,11,2006)}; ShallowDate[] e = d; Creates alias between e, d e.g.d[0] = new Date(25,12,2006);Changes e[0] also Half-deepDate[] e = new Date[d.length];for (int i = 0; i < d.length; i++)e[i] = d[i]; No aliasing at array level … … but aliasing at cell level e.g.d[0] = new Date (25,12,2006);No change to e[0] Butd[1].setMonth(1);Changes e[1] also because DeepDate [] e = new Date[d.length];for (int i = 0; i < d.length; i++)e[i] = new Date(d[i]); Note use of Date copy constructor! No dependencies between e, dCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6Shallow CopyingDate[] d = {new Date(12,10,2006), new Date(15,11,2006)};Date[] e = d;HeapStackd10/12/200611/15/2006eCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7Half-Deep CopyingDate[] d = {new Date(12,10,2006), new Date(15,11,2006)};Date[] e = new Date[d.length];for (int i=0; i < d.length, i++)e[i] = d[i];HeapStackd10/12/200611/15/2006eCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Deep CopyingDate[] d = {new Date(12,10,2006), new Date(15,11,2006)};Date [] e = new Date[d.length];for (int i = 0; i < d.length; i++)e[i] = new Date(d);HeapStackd10/12/200611/15/2006e10/12/200611/15/2006CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9When To Use What Kind of Copying?Shallow copying is (not always, but) usually a bad idea Deep copying provides maximal protection against aliasing If objects stored in array are immutable, half-deep provides same protection against aliasing as deepe.g. String arrays Storage usage Shallow: least Half-deep: middle Deep: most If storage is an issue, aliasing problems may be worth coping withCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Privacy Leaks Recall DateRange class:public class DateRange {private Date start = null; // start dateprivate Date end = null; // end date// ConstructorsDateRange (Date start, Date end){ … }DateRange (){ … }// AccessorsDate getStart (){return start;}Date getEnd (){return end;}} Instance variables are private, meaning they should only be changed using DateRangemethods Since no DateRange methods involve set, DateRange objects “should” be immutable Are they?CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Changes Dates in Date Ranges Consider following codeDateRange r = new DateRange (new Date(13,10,2006),new Date(20,10,2006));Date d = r.getStart();d.setMonth (9); After this executes, what does following print?r.getStart().println();(Answer: 9/13/2006) This phenomenon is called a privacy leak Private instance variables can be modified outside class Behavior is due to aliasingHeapStackr10/20/2006dstartend10/13/2006CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Fixing Privacy Leaks Return copies of objects referenced by instance variables To fix getStart method in DateRange:Date getStart(){return new Date(start);} This returns a copy of start date Changes made to this copy will not affect
View Full Document