Lecture 22 More on Array Operations Last time 1 Array lengths and out of bounds indexing 2 Copying arrays 3 Explicit array initialization Today 1 Arrays as arguments 2 Arrays of objects 3 Array copying 4 Privacy leaks 10 20 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Project 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 automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Arrays 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 array CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 Example public static void triple int a a 3 public static void triple int a for int i 0 i a length i a i 3 Stack Heap a a 15 5 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 15 0 5 Output 5 15 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 15 0 5 15 0 5 15 0 5 15 0 5 3 Arrays of Objects Class types can also be base types of arrays e g String acc new String 3 Array cells store references to objects Stack Heap acc Array initializers can also be used String acc UMD UNC Duke Expressions can also appear in initializers Date midterms new Date 12 10 2006 new Date 15 11 2006 UMD UNC Duke CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Three Ways of Copying Arrays of Objects Date d new Date 12 10 2006 new Date 15 11 2006 Shallow Date e d Creates alias between e d e g d 0 new Date 25 12 2006 Changes e 0 also Half deep Date 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 But d 1 setMonth 1 Changes e 1 also because Deep Date 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 d CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Shallow Copying Date d new Date 12 10 2006 new Date 15 11 2006 Stack Heap d e Date e d 10 12 2006 11 15 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 Half Deep Copying Date d new Date 12 10 2006 new Date 15 11 2006 Stack Heap d e Date e new Date d length for int i 0 i d length i e i d i 10 12 2006 11 15 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 Deep Copying Date 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 Stack Heap d e 10 12 2006 11 15 2006 10 12 2006 11 15 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 When 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 deep e g String arrays Storage usage Shallow least Half deep middle Deep most If storage is an issue aliasing problems may be worth coping with CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Privacy Leaks Recall DateRange class public class DateRange private Date start null private Date end null start date end date Constructors DateRange Date start Date end DateRange Accessors Date getStart return start Date getEnd return end Instance variables are private meaning they should only be changed using DateRange methods Since no DateRange methods involve set DateRange objects should be immutable Are they CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 Changes Dates in Date Ranges Consider following code DateRange 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 Stack Heap r d start end 10 13 2006 10 20 2006 Private instance variables can be modified outside class Behavior is due to aliasing CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Fixing Privacy Leaks Return copies of objects referenced by instance variables To fix getStart method in DateRange Date getStart return new Date start Stack Heap r d 10 13 2006 start end 10 13 2006 This returns a copy of start date Changes made to this copy will not affect original CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 20 2006 12
View Full Document