Mutators and Accessors in Compositions-avoid
privacy leaks-make
defensive copies whenever mutable objects are passed into or out of the composition. No changes are needed for immutable objects.
Mutator-create a copy of the external object and start that in our object's attribute.
Accessor-create a copy of the objects attribute and return that copy.
Constructor-just like the mutator.
Copy Constructors of Compositions and Aggregates-"
deep copy" --> respect the semantics of the internal objects.
-
composition: copy constructor also creates new copies of all internal mutable objects.
-
aggregate: only copy references, do not create new copies of internal objects.
Arrays -Array is similar to ArrayList
- a list of objects stores by index
- all objects of the same type
Array Syntax (starting with primitives) e.g. a list of 5 integers: int[] x = new int[5]; //creates an empty array of 5 integers.
x.length gives you the length of the array (5 in this case)
e.g. I want to store the int 1024 in the first element of the array.
x[0] = 1024; //note that array indices, like ArrayList, start at zero.
Say you want to get the third element in the array and store it in a separate int.
int y;
y = x[2];
Arrays can be initialized at declaration.
int[] x = {1024,345,-2,123,81};