The Artima Developer Community
Sponsored Link

Java Answers Forum
API Extension java.lang.reflect.Array - help me make it worthy!

3 replies on 1 page. Most recent reply: Jan 20, 2005 6:25 PM by lalama fodie toure

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Chris Miller

Posts: 23
Nickname: lordsauron
Registered: Jan, 2005

API Extension java.lang.reflect.Array - help me make it worthy! Posted: Jan 19, 2005 5:48 PM
Reply to this message Reply
Advertisement
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.
 */
public final class 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.
	 */
	public static float 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.
	 */
	public static float 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.
	 */
	public static float 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.
	 */
	public static float 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.
	 */
	public static float 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.
	 */
	public static float 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...
	public static native int getLength(Object array) throws IllegalArgumentException;
}
 


Chris Miller

Posts: 23
Nickname: lordsauron
Registered: Jan, 2005

Re: API Extension java.lang.reflect.Array - help me make it worthy! Posted: Jan 19, 2005 6:01 PM
Reply to this message Reply
Oops, I clicked "post" too soon... Anyway, thanks for your time. If you'd like, I could add your name in the @author line.

Chris Miller

Posts: 23
Nickname: lordsauron
Registered: Jan, 2005

Re: API Extension java.lang.reflect.Array - help me make it worthy! Posted: Jan 20, 2005 3:56 PM
Reply to this message Reply
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?"

lalama fodie toure

Posts: 11
Nickname: lalama11
Registered: Nov, 2004

Re: API Extension java.lang.reflect.Array - help me make it worthy! Posted: Jan 20, 2005 6:25 PM
Reply to this message Reply
Alright you're idiot to do this! if you will
best regard !

Flat View: This topic has 3 replies on 1 page
Topic: Final Classes in Java Previous Topic   Next Topic Topic: Java Launcher 2.4 released

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use