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:
parent
4db1390786
commit
00db5573e6
|
@ -62,7 +62,7 @@ import org.apache.commons.lang.builder.ToStringBuilder;
|
|||
import org.apache.commons.lang.builder.ToStringStyle;
|
||||
|
||||
/**
|
||||
* <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and primitive wrapper arrays
|
||||
* <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and primitive wrapper arrays
|
||||
* (like <code>Integer[]</code>).</p>
|
||||
*
|
||||
* <p>This class tries to handle <code>null</code> input gracefully.
|
||||
|
@ -78,8 +78,9 @@ import org.apache.commons.lang.builder.ToStringStyle;
|
|||
* @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 {
|
||||
|
||||
|
@ -251,7 +252,7 @@ public class ArrayUtils {
|
|||
*
|
||||
* <p>This method returns <code>null</code> if <code>null</code> array input.</p>
|
||||
*
|
||||
* @param array an array whose elements are either a {@link java.util.Map.Entry} or
|
||||
* @param array an array whose elements are either a {@link java.util.Map.Entry} or
|
||||
* an Array containing at least two elements, may be <code>null</code>
|
||||
* @return a <code>Map</code> that was created from the array
|
||||
* @throws IllegalArgumentException if one element of this Array is
|
||||
|
@ -272,13 +273,13 @@ public class ArrayUtils {
|
|||
} else if (object instanceof Object[]) {
|
||||
Object[] entry = (Object[]) object;
|
||||
if (entry.length < 2) {
|
||||
throw new IllegalArgumentException("Array element " + i + ", '"
|
||||
throw new IllegalArgumentException("Array element " + i + ", '"
|
||||
+ object
|
||||
+ "', has a length less than 2");
|
||||
}
|
||||
map.put(entry[0], entry[1]);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Array element " + i + ", '"
|
||||
throw new IllegalArgumentException("Array element " + i + ", '"
|
||||
+ object
|
||||
+ "', is neither of type Map.Entry nor an Array");
|
||||
}
|
||||
|
@ -435,6 +436,44 @@ public class ArrayUtils {
|
|||
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 (<0)
|
||||
* is promoted to 0, overvalue (>array.length) results
|
||||
* in an empty array.
|
||||
* @param endIndex elements upto endIndex-1 are present in the
|
||||
* returned subarray. Undervalue (< startIndex) produces
|
||||
* empty array, overvalue (>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
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
|
|
|
@ -71,7 +71,7 @@ import junit.textui.TestRunner;
|
|||
* @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 class ArrayUtilsTest extends TestCase {
|
|||
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;
|
||||
|
|
Loading…
Reference in New Issue