Hi I wrote a class that carries out operations on integer-type-static-arrays. However, I'd like to extend the functionalities in this class to other primitive-type-static-arrays, but I don't know a short way to do this without having to create new classes for each primitive type.
Please let me know if this is possible; and how do I go about it?
Here's the code for the class:
Please let me know if this is possible; and how do I go about it?
Here's the code for the class:
import acm.util.ErrorException;
/**
* @author ac3Takwas
*
* This class defines a number of operations that can be
* carried out on static array integer types.
* Most of these operations are similar to the ones
* in the java.util.ArrayList class
*
* Have Fun Using them!
*/
class ArrayOperation {
private static final int NOT_FOUND = -1;
/**
* gets a sub-array of the specified array that contains
* elements between the specified index and the end of the array
*
* @param arr the big array from which a new array is to be formed
* @param startIndex the index at which to start the selection
* @return the new sub-array; null if the specified index is invalid
*/
public static int[] subStartArray(int[] arr, int startIndex){
return subArray(arr, startIndex, arr.length);
}
/**
* gets a sub-array of the specified array that contains
* elements between the beginning of the array and the specified index
*
* @param arr the big array from which a new array is to be formed
* @param endIndex the index at which to end the selection
* @return the new sub-array; null if the specified index is invalid
*/
public static int[] subEndArray(int[] arr, int endIndex){
return subArray(arr, 0, endIndex);
}
/**
* Gets a sub-array of the passed array between the specified indices;
* exclusive of the value at the endIndex
*
* @param arr the passed array (parent array) from which the sub-array is to be derived
* @param index the index of the parent array from which to start
* @param endIndex the index to stop; the value located at this index is not included in the returned sub-array
* @return the derived sub-array if found; null if any of the specified indexes is invalid
*/
public static int[] subArray(int[] arr, int index, int endIndex){
if(index<0 || index>arr.length || endIndex<0 || endIndex>arr.length)
return null;
else{
int[] temp = new int[(endIndex-index)];
for(int i=0; i<temp.length; i++){
temp[i] = arr[index++];
}
return temp;
}
}
/**
* Gets the index position of the first occurrence of the specified value in the specified array
*
* @param arr the array from which to find the value
* @param value the value to search for
* @return the index of the value if found; -1 if the value is not contained within the array
*/
public static int indexOf(int[] arr, int value){
for(int i=0; i<arr.length; i++){
if(arr[i] == value)
return i;
}
return NOT_FOUND;
}
/**
* Gets the index position of the last occurrence of the specified value in the specified array
*
* @param arr the array from which to find the value
* @param value the value to search for
* @return the index of the value if found; -1 if the value is not contained within the array
*/
public static int lastIndexOf(int[] arr, int value){
for(int i=arr.length-1; i>=0; i--){
if(arr[i] == value)
return i;
}
return NOT_FOUND;
}
/**
* Finds out if the specified array contains the specified
*
* @param arr the array from which to search
* @param value the value to search for
* @return true if the array contains the value; false if the value is not contained within the array
*/
public static boolean contains(int[] arr, int value){
for(int i=0; i<arr.length; i++){
if(arr[i] == value)
return true;
}
return false;
}
/**
* adds the specified value to the specified array thereby expanding the array to allow it hold this new value
*
* @param arr the array to which to add a value
* @param value the value to add to the array
* @return a new array that has the specified value as one of it's contents
*/
public static int[] add(int[] arr, int value){
int[] temp = {value};
return combineArrays(arr, temp);
}
/**
* adds the specified value to the specified array in the specified index
*
* @param arr the array to add the a new value to
* @param value the value to add to the array
* @param index the index to which to add the new value
* @return a new array that has the specified value as one of it's contents and located at the specified array
* @throws ErrorException if the specified index position is wrong
*/
public static int[] addToIndex(int[] arr, int value, int index)throws ErrorException{
if(index<0 || index>arr.length ){
System.out.print("\nThat's an invalid index");
throw new ErrorException("That's an invalid index");
}
int[] tempBeg = add(subEndArray(arr, index), value);
int[] tempEnd = subStartArray(arr, index);
int[] temp = combineArrays(tempBeg, tempEnd);
return temp;
}
/**
* Removes the first occurrence of the specified value from the specified array
*
* @param arr the array from which to remove the value
* @param value the value to remove
* @return a new (reduced) array
*/
public static int[] removeFirstOccurrence(int[] arr, int value){
int[] tempBeg = subEndArray(arr, indexOf(arr, value));
int[] tempEnd = subStartArray(arr, indexOf(arr, value)+1);
return combineArrays(tempBeg, tempEnd);
}
/**
* Removes all occurrence of the specified value from the specified array
*
* @param arr the array from which to remove the value
* @param value the value to remove
* @return a new (reduced) array that no longer has any occurrence of the specified value
*/
public static int[] removeAllOccurrence(int[] arr, int value){
while( indexOf(arr, value) != NOT_FOUND){
arr = removeFirstOccurrence(arr, value);
}
return arr;
}
/**
* Combines two arrays to form a new array
*
* @param arr1 the first array
* @param arr2 the second array
* @return a new array that contains all values from both the first and second arrays
*/
public static int[] combineArrays(int[] arr1, int[] arr2){
if(arr1 == null)
return arr2;
else if (arr2 == null) {
return arr1;
}
else{
int[] temp = new int[arr1.length + arr2.length];
int tempArrTracker = 0;
for(int i=0; i<arr1.length; i++){
temp[tempArrTracker++] = arr1[i];
}
for(int i=0; i<arr2.length; i++){
temp[tempArrTracker++] = arr2[i];
}
return temp;
}
}
/**
* Replaces all occurrences of the specified value in the specified
* array with a new specified value
*
* @param arr the array on which to carry out the operation
* @param value the value to be replaced
* @param replacer the new value with which to replace the old value
* @return a new edited array
*/
public static int[] replaceAllOccurrence(int[] arr, int value, int replacer){
for(int i=0; i<arr.length; i++){
if(arr[i] == value)
arr[i] = replacer;
}
return arr;
}
/**
* Sorts the specified array in an ascending order
*
* @param arr the array to be sorted
* @return a new sorted array
*/
public static int[] sortAscending(int[] arr){
return reverse(sortDescending(arr));
}
/**
* Re-arranges the specified array by reversing it
*
* @param arr the array to be reversed
* @return a new reversed array
*/
public static int[] reverse(int[] arr){
int[] temp = new int[arr.length];
for(int i= arr.length-1, j=0; i>=0; i--, j++){
temp[j] = arr[i];
}
return temp;
}
/**
* Sorts the specified array in an descending order
*
* @param arr the array to be sorted
* @return a new sorted array
*/
public static int[] sortDescending(int[] arr){
for (int leftSide= 0; leftSide<arr.length; leftSide++){
int[] temp = subStartArray(arr, leftSide);
int currentMax = getMax(temp);
if(arr[leftSide] < currentMax){
arr = swap(arr, leftSide, leftSide+indexOf(temp, currentMax));
}
}
return arr;
}
private static int getMax(int[] arr) {
int max = 0;
for(int i=0; i<arr.length; i++){
max = Math.max(max, arr[i]);
}
return max;
}
/**
* Swaps the values in the specified indexes
*
* @param arr the array on which to carry out the operation
* @param index1
* @param index2
* @return
*/
public static int[] swap(int[] arr, int index1, int index2){
int temp = arr[index1];
arr[index1] = arr[index2];;
arr[index2] = temp;
return arr;
}
}