I just made the beta for a extension to the java.lang.reflect.Arrays class in the API, giving it the ability to take an array of numbers (ie short, int, etc.) and return the average. However, I'm relatively new to java, and I want to make this code as optimized as possible so I can send it to Sun in hopes of getting it into the next JDK. I stripped down the Array class, though, to only the stuff that was really applicable, so if you see stuff missing, it's because I was trying to conserve space, and keep focused on the content at hand. Here's the code:
/**
* <code>Math</code>
* The Bug Splat Softworks proposed addition to the Java API
* @author Bug Splat Softworks
*/
package java.lang.reflect;
/**
* There's a lot of other Sun inventions that are irrelevant to what I'm proposing;
* for the sake of saving time, existing methods are not in this file - this file
* is for the sole purpose of displaying possible additions to the existing Java
* API and that only. Information from the Java API source code was utilized to
* create this code. The copy of the source was legally downloaded, and to the
* knowledge of Bug Splat Softworks has not been abused or illegally used in any
* way.
*/
publicfinalclass Array{
private Array(){} // This was added to get the compiler to stop complaining.
/**
* <code>public static float average(byte[] byteArray)</code>
* Returns the <code>float</code>-formatted average of the unidimensional array of <code>byte</code>s.
*/
publicstaticfloat average(byte[] byteArray){
int inArray = 0, left = getLength(byteArray); float total = 0;
while(left != 0){
total = total + (float)byteArray[inArray]; inArray++; left--;
} return(total / getLength(byteArray));
}
/**
* <code>public static float average(short[] shortArray)</code>
* Returns the <code>float</code>-formatted average of the unidimensional array of <code>short</code>s.
*/
publicstaticfloat average(short[] shortArray){
int inArray = 0, left = getLength(shortArray); float total = 0;
while(left != 0){
total = total + (float)shortArray[inArray]; inArray++; left--;
} return(total / getLength(shortArray));
}
/**
* <code>public static float average(int[] intArray)</code>
* Returns the <code>float</code>-formatted average of the unidimensional array of <code>int</code>s.
*/
publicstaticfloat average(int[] intArray){
int inArray = 0, left = getLength(intArray); float total = 0;
while(left != 0){
total = total + (float)intArray[inArray]; inArray++; left--;
} return(total / getLength(intArray));
}
/**
* <code>public static float average(long[] longArray)</code>
* Returns the <code>float</code>-formatted average of the unidimensional array of <code>long</code>s.
*/
publicstaticfloat average(long[] longArray){
int inArray = 0, left = getLength(longArray); float total = 0;
while(left != 0){
total = total + (float)longArray[inArray]; inArray++; left--;
} return(total / getLength(longArray));
}
/**
* <code>public static float average(float[] floatArray)</code>
* Returns the <code>float</code>-formatted average of the unidimensional array of <code>float</code>s.
*/
publicstaticfloat average(float[] floatArray){
int inArray = 0, left = getLength(floatArray); float total = 0;
while(left != 0){
total = total + (float)floatArray[inArray]; inArray++; left--;
} return(total / getLength(floatArray));
}
/**
* <code>public static float average(double[] doubleArray)</code>
* Returns the <code>float</code>-formatted average of the unidimensional array of <code>double</code>s.
*/
publicstaticfloat average(double[] doubleArray){
int inArray = 0, left = getLength(doubleArray); float total = 0;
while(left != 0){
total = total + (float)doubleArray[inArray]; inArray++; left--;
} return(total / getLength(doubleArray));
}
// This copied from the API source because it was necessary to include,
// otherwise the program doesn't compile correctly - guess what that does to
// testing and debugging. Since we're pretending this is the Array class,
// we had to include the Array stuff this way because we can't technically
// import ourselves, and if we did, we wouldn't have what we wanted...
publicstaticnativeint getLength(Object array) throws IllegalArgumentException;
}
All right... I know you don't like me but this kind of lack-of-remarks is unnerving... Isn't anyone even going to say "Uh, you're an idiot for doing something like this?"