Add reverse to ArrayUtils

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137165 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2002-11-24 16:50:38 +00:00
parent 28f37f2b4d
commit 6b8ba82f14
2 changed files with 332 additions and 2 deletions

View File

@ -67,7 +67,7 @@
* @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a>
* @author Moritz Petersen
* @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
* @version $Id: ArrayUtils.java,v 1.5 2002/11/16 12:56:43 scolebourne Exp $
* @version $Id: ArrayUtils.java,v 1.6 2002/11/24 16:50:38 scolebourne Exp $
*/
public class ArrayUtils {
@ -679,4 +679,213 @@ public static boolean isSameType(Object array1, Object array2) {
return array1.getClass().getName().equals(array2.getClass().getName());
}
/**
* Reverses the order of the given array.
* <p>
* There is no special handling for multi-dimensional arrays.
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
*/
public static void reverse(Object[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
Object tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* Reverses the order of the given array.
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
*/
public static void reverse(long[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
long tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* Reverses the order of the given array.
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
*/
public static void reverse(int[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
int tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* Reverses the order of the given array.
* <p>
* There is no special handling for multi-dimensional arrays.
*
* @param array the array to reverse
*/
public static void reverse(short[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
short tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* Reverses the order of the given array.
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
*/
public static void reverse(char[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
char tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* Reverses the order of the given array.
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
*/
public static void reverse(byte[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
byte tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* Reverses the order of the given array.
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
*/
public static void reverse(double[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
double tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* Reverses the order of the given array.
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
*/
public static void reverse(float[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
float tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
/**
* Reverses the order of the given array.
* <p>
* The method does nothing if <code>null</code> is passed in.
*
* @param array the array to reverse
*/
public static void reverse(boolean[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
boolean tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
}

View File

@ -66,7 +66,7 @@
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author Moritz Petersen
* @version $Id: ArrayUtilsTest.java,v 1.2 2002/11/16 12:56:44 scolebourne Exp $
* @version $Id: ArrayUtilsTest.java,v 1.3 2002/11/24 16:50:38 scolebourne Exp $
*/
public class ArrayUtilsTest extends TestCase {
@ -517,4 +517,125 @@ public void testSameType() {
assertEquals(false, ArrayUtils.isSameType(new String[0][0], new String[0]));
}
//-----------------------------------------------------------------------
public void testReverse() {
StringBuffer str1 = new StringBuffer("pick");
String str2 = "a";
String[] str3 = new String[] {"stick"};
String str4 = "up";
Object[] array = new Object[] {str1, str2, str3};
ArrayUtils.reverse(array);
assertEquals(array[0], str3);
assertEquals(array[1], str2);
assertEquals(array[2], str1);
array = new Object[] {str1, str2, str3, str4};
ArrayUtils.reverse(array);
assertEquals(array[0], str4);
assertEquals(array[1], str3);
assertEquals(array[2], str2);
assertEquals(array[3], str1);
array = null;
ArrayUtils.reverse(array);
assertEquals(null, array);
}
public void testReverseLong() {
long[] array = new long[] {1L, 2L, 3L};
ArrayUtils.reverse(array);
assertEquals(array[0], 3L);
assertEquals(array[1], 2L);
assertEquals(array[2], 1L);
array = null;
ArrayUtils.reverse(array);
assertEquals(null, array);
}
public void testReverseInt() {
int[] array = new int[] {1, 2, 3};
ArrayUtils.reverse(array);
assertEquals(array[0], 3);
assertEquals(array[1], 2);
assertEquals(array[2], 1);
array = null;
ArrayUtils.reverse(array);
assertEquals(null, array);
}
public void testReverseShort() {
short[] array = new short[] {1, 2, 3};
ArrayUtils.reverse(array);
assertEquals(array[0], 3);
assertEquals(array[1], 2);
assertEquals(array[2], 1);
array = null;
ArrayUtils.reverse(array);
assertEquals(null, array);
}
public void testReverseChar() {
char[] array = new char[] {'a', 'f', 'C'};
ArrayUtils.reverse(array);
assertEquals(array[0], 'C');
assertEquals(array[1], 'f');
assertEquals(array[2], 'a');
array = null;
ArrayUtils.reverse(array);
assertEquals(null, array);
}
public void testReverseByte() {
byte[] array = new byte[] {2, 3, 4};
ArrayUtils.reverse(array);
assertEquals(array[0], 4);
assertEquals(array[1], 3);
assertEquals(array[2], 2);
array = null;
ArrayUtils.reverse(array);
assertEquals(null, array);
}
public void testReverseDouble() {
double[] array = new double[] {0.3d, 0.4d, 0.5d};
ArrayUtils.reverse(array);
assertEquals(array[0], 0.5d, 0.0d);
assertEquals(array[1], 0.4d, 0.0d);
assertEquals(array[2], 0.3d, 0.0d);
array = null;
ArrayUtils.reverse(array);
assertEquals(null, array);
}
public void testReverseFloat() {
float[] array = new float[] {0.3f, 0.4f, 0.5f};
ArrayUtils.reverse(array);
assertEquals(array[0], 0.5f, 0.0f);
assertEquals(array[1], 0.4f, 0.0f);
assertEquals(array[2], 0.3f, 0.0f);
array = null;
ArrayUtils.reverse(array);
assertEquals(null, array);
}
public void testReverseBoolean() {
boolean[] array = new boolean[] {false, false, true};
ArrayUtils.reverse(array);
assertEquals(array[0], true);
assertEquals(array[1], false);
assertEquals(array[2], false);
array = null;
ArrayUtils.reverse(array);
assertEquals(null, array);
}
}