






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
1
Data structures: mechanisms for storing data in a structured way We have seen simple data structures implemented as classes: Rational.java Rational number data stored as numerator / denominator pair Arrays are a very useful data structure provided by Java and other programming languages Array: sequence of variables of the same type homogeneous data structure size (quantity) fixed when space is allocated ordered Individual elements of sequence can be referenced/updated/etc. Arrays are objects (hence allocated on heap) with a reference on the stack Like other objects, “instance variables” of array = cells in array are assigned default values (0 / null / etc.) when array created
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
2
Array Indexing
Declaration of a: int[] a; Allocation of space for array named a: a = new int[5]; This creates five int variables “named”::a[0],a[1],a[2],a[3],a[4] To modify contents of cell #2 to 6 and cell #1 to 74: a[2] = 6; a[1] = 74; To use the contents of cell #2 and cell #1 : System.out.println(“value = “ + (a[1]-a[2]));
In Java / C / C++, array cells are indexed beginning at 0 and going up to n-1 (n is number of cells) Beware: start at 0! and end at one less than the size!!
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
3
Square Brackets: [ ] and length
Three uses in Java:
Array variable declaration
int[] a;
Array object creation
new int[10];
Array indexing a[0]
array also has a.length holds the amount
of space currently allocated for that array
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
6
A Common Programming
Idiom
To process all elements in array a…
Do following:
Use fresh loop counter to avoid overwriting another variable of same name elsewhere
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
7
Copying Arrays
Does the following copy a into b?
How to make a copy?
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
8
Making Arrays Bigger
Suppose we want to make an array bigger by
adding an element.
Does the following work?
No!
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The final field array.length cannot be assigned at Sample.main(Sample.java:15)
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
9
To Make an Array Bigger…
int[] a = new int[5]; { int[] temp = new int[a.length + 1]; for (int i = 0; i < a.length; i++){ temp[i] = a[i]; } a = temp; } New variable temp created to hold copy New block created to ensure temp does not interfere with another variable of the same name Previous contents of a become garbage
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
12
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
Array initializers can also be used String[] acc = {“UMD”, “UNC”, “Duke”};
Stack Heap
acc
UMD
UNC
Duke
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
13
Arrays of Objects (continued)
Cat[] kennel = { new Cat(“Joe”), new Cat(“Jill”), new Cat(“Fluffy”) };
Stack Heap kennel
Joe
Jill
Fluffy
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
14
Foo object
Privacy Leaks
public class MutableThing { … public void mutateMe() {…}; } public class Foo { private MutableThing q = new MutableThing(); … public MutableThing getQ(){ return q; } } Consider following code Foo f = new Foo (); MutableThing m = f.getQ(); m.mutateMe(); After this executes, what happens? This phenomenon is called a privacy leak Private instance variables can be modified outside class Behavior is due to aliasing
Stack Heap
f m
MutableThing object
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
15
Fixing Privacy Leaks
MutableThing getQ(){ return new MutableThing(q); }
Stack Heap f m
Foo object
MutableThing object
MutableThing object
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
18
Deep Copying
Person[] d = {
new Person(2.1,7,…), new Person(3.3,2,…)
};
Person[] e = new Person[d.length];
for (int i=0; i<d.length; i++) {
e[i] = new Person(d[i]);
}
Stack Heap
d e
2
7
2
7
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
19
Three Ways of Copying
Reference copy public ReCD[] getCDsReferenceCopy() { return myFavorites; } Shallow copy public ReCD[] getCDsShallowCopy() { ReCD[] copy = new ReCD[myFavorites.length]; for ( int i = 0; i < copy.length; i++) copy[i] = myFavorites[i]; return copy; } Deep copy public ReCD[] getCDsDeepCopy() { ReCD[] copy = new ReCD[myFavorites.length]; for ( int i = 0; i < copy.length; i++) copy[i] = new ReCD(myFavorites[i]); return copy; }
ReCDCollectionOwner p = new RECD…; ReCD[] a = p.getCD…(); a[0] = otherCDalreadycreated; a[0].rewrite(“other”,”name”);
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
20
When To Use What Kind of
Copying?
Reference copying is usually a bad idea (not always but realize what you are doing)
Deep copying provides maximal protection against aliasing (but takes a lot of time and space if it was not necessary)
Storage space and time used
If the class is mutable, aliasing is something to be avoided and you must have true copies to prevent privacy leaks and modifications outside.
If you know the class is immutable, aliasing doesn’t hurt but neither does making true copies (except wasted space and time).
If storage is an issue, aliasing problems may be worth coping with but must be well documented.