Skip to content

Commit

Permalink
ArrayUtils: Added add(double[], double)
Browse files Browse the repository at this point in the history
  • Loading branch information
reportmill committed Sep 2, 2021
1 parent 0fbb1cc commit 6e112a1
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/snap/util/ArrayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,33 @@ public static <T> T[] removeId(T anArray[], T anObj)
return index>=0 ? remove(anArray, index) : anArray;
}

/**
* Returns new double array by adding given value to given array.
*/
public static double[] add(double[] anArray, double anObj) { return add(anArray, anObj, anArray.length); }

/**
* Returns new double array by adding given value to given array at given index.
*/
public static double[] add(double[] anArray, double anObj, int anIndex)
{
// Get array length (throw exception if index out of bounds)
int length = anArray.length;
if (anIndex < 0 || anIndex > length)
throw new ArrayIndexOutOfBoundsException(anIndex);

// Copy array with extra space
double[] newArray = Arrays.copyOf(anArray, length + 1);

// Copy elements prior to index, add value at index, and copy elements beyond index
newArray[anIndex] = anObj;
if (anIndex < length)
System.arraycopy(anArray, anIndex, newArray, anIndex + 1, length - anIndex);

// Return new array
return newArray;
}

/**
* Returns the number of object in array of a given class.
*/
Expand Down

0 comments on commit 6e112a1

Please sign in to comment.