Lab2 1DArrays
Lab2 1DArrays
The System.arraycopy method & the Arrays.copyOf method can help you with these
operations. Look these up in the JavaDoc webpages to clarify the parameter details.
The method is passed a collection of integers and a single integer value. The method
returns true if the collection contains the integer value. Otherwise, it returns false.
// Now step through the array until one of two things happens.
// (i) We exhaust all the items in the array (i.e. we reach the end
// and have not found the 'x'
// (ii) We find 'x' at some position in the array (i.e. we refute the
// initial assumption.
// Because we need the value if 'i' outside the loop it has been
// declared separately.
int i;
// Step through the array from beginning to end (or first to last)
for(i = 0 ; i < a.length && a[i] != x ; i++);
// We have stopped stepping through the array. Did we find 'x'?
// If we reached the end of the array then WE DID NOT.
// If we stopped BEFORE we reached then end then WE DID.
// Check which one it is
if(i < a.length){
// We DIDN'T reach the end of the array - So we found 'x' at
position 'i'
// Change our initial assumption
found = true;
}
// Return the FINAL assumption
return found ;
}
//
CS4222: Lab 1: Arrays
The method is passed a collection of integers, an integer value that we want to search
for and an integer value specifying the maximum number of array positions to be
searched. This method is like the method in exercise 1 except that exercise 1 searches
the entire array whereas this method searches only the first N positions (i.e., positions 0
to N-1) of the array. The size of the section that is searched is specified by the third
parameter N.
If the collection contains the integer value in any of the first N positions the method
returns true. Otherwise it returns false.
// Now step through the array until one of two things happens.
// (i) We exhaust all the items in the array at positions 0 to N-1
(i.e. we reach
// position N and we have not found the value 'x'
// (ii) We find 'x' at one of the positions 0 to N-1 (i.e. we refute
the
// initial assumption.
// Because we need the value if 'i' outside the loop it has been
// declared separately.
int i;
// Step through the array from beginning to end (or first to last)
for(i = 0 ; i < N && a[i] != x ; i++);
// We have stopped stepping through the array. Did we find 'x'?
// If we reached the end of the array then WE DID NOT.
// If we stopped BEFORE we reached then end then WE DID.
// Check which one it is
if(i < N){
// We DIDN'T reach position N - So we found 'x' at position 'i'
// Change our initial assumption
found = true;
}
CS4222: Lab 1: Arrays
// Return the FINAL assumption
return found ;
}
//
The method is passed a collection of integers and an integer value. The method returns
a collection that contains the positions of all occurrences of the value.
The collection returned cannot be longer than the length of the collection passed to the
method. Start with a new collection that is large enough and, if necessary, resize it after
you have established how many occurrences of x are in the collection.
// We don't yet know how many times the value 'x' appears in the
// array 'a' so initially we assume that it appears in EVERY one of
// the positions. We will (if necessary) change this later.
// Step through the array looking for values of 'x'. Each time we find
// one we record the position, or index, in the indices array. We also
// increment the 'xOccurrenceCount' to note how many we have found BUT
ALSO
// to inform us where the next index should be recorded in the
'indices' array.
// Think carefully about that last sentence. A very, very, very useful
side effect
// of counting the occurrences is that we also know where the next
index
// should be stored in the 'indices' array.
//
for(int i=0 ; i < a.length ; i++) {
if(a[i] == x) {
indices[xOccurrenceCount] = i;
CS4222: Lab 1: Arrays
xOccurrenceCount++;
}
}
// Our initial assumption about the space requirements for the
'indices' array
// MAY have been too generous. We need to check if the utilisation of
the
// array is less than the capacity. If it is we will remove the excess
capacity
// and return a "trimmed" array that is exactly the correct size for
the number
// of 'indices'.
if(xOccurrenceCount < indices.length) {
// To 'resize' or 'trim' the 'indices' array we use the
Arrays.copyOf method which
// creates a new array of the correct size, copies the data to it,
and returns the
// reference to the new (smaller) array
indices = Arrays.copyOf(indices,xOccurrenceCount);
}
//
// Alternatively, we could have written the code ourselves as follows
// if(xOccurrenceCount < indices.length) {
// int[] trimmedArray = new int[xOccurrenceCount];
// // Copy ONLY the required entries (i.e. from position 0 to
the position
// // identified by the variable 'xOccurrenceCount') from the
'indices' array
// // to the corresponding positions in the new
'trimmedArray'.
// for(int i=0 ; i < utilisation ; i++) {
// trimmedArray[i] = indices[i];
// }
// // NOTE, now 'trimmedArray' is FULL, or viewed
// // another way, there is no unused/wasted space in it.
// }
// // Now set 'indices' to refer to the 'trimmedArray' space
// indices = trimmedArray;
//
// Now return the 'indices' array the contains the indices of x
return indices;
}
//
The method is passed TWO collections of integers. It returns a SINGLE collection which
contains all of the values in the first collection (i.e. collection a) followed by all of the
values in the second collection (i.e. collection b).
The method is passed TWO collections of integers and returns the union of the two
collections (i.e. the values from both collections but with duplicates removed).
HINT: The union of the two collections cannot be longer than the combined lengths of
the two collections. You could start with a collection that is the size of the combined
lengths and, if necessary, resize it after you have established the values that are in the
union.
The method is passed TWO collections of integers and returns the intersection of the
two collections (i.e. the values that appear in BOTH collections).
HINT: The intersection of the two collections cannot be longer than the length of the
shortest collection. You could start with a collection that size and, if necessary, resize it
after you have established the number of values that are in the intersection.
CS4222: Lab 1: Arrays