- Declaring
- double[] testmark;
- Assign Space
- testMark = new double[5]
- Use Elements in Array
- testMark[3] = 720;
- an array is a table of values stored in memory
- the array name (testMark) is a reference to the array, just like object references
- to refer the ith member of the array
- testMark[i]
- Very Important
- array indices start at zero, not one.
- use testMark.length
- length is the keyword to get the length of the array
- arrays are objects
- they should be treated as such
- because we can change the elements of an array and because arrays are objects we have to treat them as mutable objects
- deep copy, defensive copy
- look again at testMark
double[]copyMarks; // create a copy of testMarks
copyMarks = testmarks //BAD!!!
- both references (copyMarks and testMarks) point to the same thing
- remember this also happens with object references
- how do we create a copy of an array?
- allocate new space
- individually copy elements
testMark = new double[5]
int i;
for (i=0, i < testMark.length, i++)
{
copyMark[i] = testMark[i];
}
- Summary
- Arrays are mutable objects which means Defensive copying
- copy on object containing an array
- deep copy
- so i do deep copy test mark
- testMark == copyMark
- this is not the same reference
- unfortunately equals() does not exist for arrays
- there exist java utilities for array equals
- java.util.arrays
- equals
- fill
- sort
2 comments:
hey Jake,
is this Friday, Feb 9th lecture??
Yeah i Believe the Arrays part is, i checked my notes and i beleive i missed this part when i originally posted, either way it's very important so i think i'm going to leave it as a seperate post
Post a Comment