1 package net.sf.openrocket.util;
3 import java.lang.reflect.Array;
5 public class ArrayUtils {
7 public static <T> T[] copyOf( T[] original, int length ) {
8 return copyOfRange(original,0,length);
12 * Implementation of java.util.Arrays.copyOfRange
14 * Since Froyo does not include this function it must be implemented here.
21 public static <T> T[] copyOfRange( T[] original, int start, int end ) {
23 if ( original == null ) {
24 throw new NullPointerException();
27 if ( start < 0 || start > original.length ) {
28 throw new ArrayIndexOutOfBoundsException();
32 throw new IllegalArgumentException();
35 T[] result = (T[]) Array.newInstance( original.getClass().getComponentType(), end-start );
38 int stop = original.length < end ? original.length : end;
39 for ( int i = start; i < stop; i ++ ) {
40 if ( i < original.length ) {
41 result[index] = original[i];
50 public static double[] copyOf( double[] original, int length ) {
51 return copyOfRange(original,0,length);
54 public static double[] copyOfRange( double[] original, int start, int end ) {
56 if ( original == null ) {
57 throw new NullPointerException();
60 if ( start < 0 || start > original.length ) {
61 throw new ArrayIndexOutOfBoundsException();
65 throw new IllegalArgumentException();
68 double[] result = new double[(end-start)];
71 int stop = original.length < end ? original.length : end;
72 for ( int i = start; i < stop; i ++ ) {
73 if ( i < original.length ) {
74 result[index] = original[i];
83 public static byte[] copyOf( byte[] original, int length ) {
84 return copyOfRange(original,0,length);
87 public static byte[] copyOfRange( byte[] original, int start, int end ) {
89 if ( original == null ) {
90 throw new NullPointerException();
93 if ( start < 0 || start > original.length ) {
94 throw new ArrayIndexOutOfBoundsException();
98 throw new IllegalArgumentException();
101 byte[] result = new byte[(end-start)];
104 int stop = original.length < end ? original.length : end;
105 for ( int i = start; i < stop; i ++ ) {
106 if ( i < original.length ) {
107 result[index] = original[i];