Add subArray method to get a portion of an array

from Ash


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137713 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-11-29 12:56:16 +00:00
parent 4db1390786
commit 00db5573e6
2 changed files with 88 additions and 6 deletions

View File

@ -78,8 +78,9 @@
* @author Tim O'Brien
* @author Pete Gieser
* @author Gary Gregory
* @author Ash
* @since 2.0
* @version $Id: ArrayUtils.java,v 1.25 2003/08/22 17:25:33 ggregory Exp $
* @version $Id: ArrayUtils.java,v 1.26 2003/11/29 12:56:15 scolebourne Exp $
*/
public class ArrayUtils {
@ -435,6 +436,44 @@ public static boolean[] clone(final boolean[] array) {
return (boolean[]) array.clone();
}
// Subarrays
//-----------------------------------------------------------------------
/**
* <p>Produces a new array containing the elements between
* the start and end indices.</p>
*
* <p>The start index is inclusive, the end index exclusive.
* Null array input produces null output.
* The result is always an <code>Object[]</code> instance</p>
*
* @param array the array
* @param startIndex the starting index. Undervalue (&lt;0)
* is promoted to 0, overvalue (&gt;array.length) results
* in an empty array.
* @param endIndex elements upto endIndex-1 are present in the
* returned subarray. Undervalue (&lt; startIndex) produces
* empty array, overvalue (&gt;array.length) is demoted to
* array length.
*/
public static Object[] subArray(Object[] array, int startIndexInclusive, int endIndexExclusive) {
if (array == null) {
return null;
}
if (startIndexInclusive < 0) {
startIndexInclusive = 0;
}
if (endIndexExclusive > array.length) {
endIndexExclusive = array.length;
}
int newSize = endIndexExclusive - startIndexInclusive;
if (newSize <= 0) {
return EMPTY_OBJECT_ARRAY;
}
Object[] subArray = new Object[newSize];
System.arraycopy(array, startIndexInclusive, subArray, 0, newSize);
return subArray;
}
// Is same length
//-----------------------------------------------------------------------
/**

View File

@ -71,7 +71,7 @@
* @author Nikolay Metchev
* @author Matthew Hawthorne
* @author Tim O'Brien
* @version $Id: ArrayUtilsTest.java,v 1.14 2003/10/11 19:58:40 tobrien Exp $
* @version $Id: ArrayUtilsTest.java,v 1.15 2003/11/29 12:56:16 scolebourne Exp $
*/
public class ArrayUtilsTest extends TestCase {
@ -271,6 +271,49 @@ public void testCloneFloat() {
assertTrue(original != cloned);
}
//-----------------------------------------------------------------------
public void testSubArray() {
Object[] inarray = { "a", "b", "c", "d", "e", "f"};
assertEquals("0 start, mid end", "abcd",
StringUtils.join(ArrayUtils.subArray(inarray, 0, 4)));
assertEquals("0 start, length end", "abcdef",
StringUtils.join(ArrayUtils.subArray(inarray, 0, inarray.length)));
assertEquals("mid start, mid end", "bcd",
StringUtils.join(ArrayUtils.subArray(inarray, 1, 4)));
assertEquals("mid start, length end", "bcdef",
StringUtils.join(ArrayUtils.subArray(inarray, 1, inarray.length)));
assertNull("null input", ArrayUtils.subArray(null, 0, 3));
assertEquals("empty array", "",
StringUtils.join(ArrayUtils.subArray(ArrayUtils.EMPTY_OBJECT_ARRAY, 1, 2)));
assertEquals("start > end", "",
StringUtils.join(ArrayUtils.subArray(inarray, 4, 2)));
assertEquals("start == end", "",
StringUtils.join(ArrayUtils.subArray(inarray, 3, 3)));
assertEquals("start undershoot, normal end", "abcd",
StringUtils.join(ArrayUtils.subArray(inarray, -2, 4)));
assertEquals("start overshoot, any end", "",
StringUtils.join(ArrayUtils.subArray(inarray, 33, 4)));
assertEquals("normal start, end overshoot", "cdef",
StringUtils.join(ArrayUtils.subArray(inarray, 2, 33)));
assertEquals("start undershoot, end overshoot", "abcdef",
StringUtils.join(ArrayUtils.subArray(inarray, -2, 12)));
// object-level tests
assertSame("empty array, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
ArrayUtils.subArray(ArrayUtils.EMPTY_OBJECT_ARRAY, 1, 2));
assertSame("start > end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
ArrayUtils.subArray(inarray, 4, 1));
assertSame("start > end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
ArrayUtils.subArray(inarray, 33, 1));
assertSame("start == end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
ArrayUtils.subArray(inarray, 3, 3));
assertSame("start overshoot, any end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY,
ArrayUtils.subArray(inarray, 8733, 4));
}
//-----------------------------------------------------------------------
public void testSameLength() {
Object[] nullArray = null;