下面列出了怎么用java.lang.ArrayIndexOutOfBoundsException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Copies characters from this string into the destination character
* array.
* <p>
* The first character to be copied is at index {@code srcBegin};
* the last character to be copied is at index {@code srcEnd-1}
* (thus the total number of characters to be copied is
* {@code srcEnd-srcBegin}). The characters are copied into the
* subarray of {@code dst} starting at index {@code dstBegin}
* and ending at index:
* <blockquote><pre>
* dstBegin + (srcEnd-srcBegin) - 1
* </pre></blockquote>
*
* @param srcBegin index of the first character in the string
* to copy.
* @param srcEnd index after the last character in the string
* to copy.
* @param dst the destination array.
* @param dstBegin the start offset in the destination array.
* @exception IndexOutOfBoundsException If any of the following
* is true:
* <ul><li>{@code srcBegin} is negative.
* <li>{@code srcBegin} is greater than {@code srcEnd}
* <li>{@code srcEnd} is greater than the length of this
* string
* <li>{@code dstBegin} is negative
* <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than
* {@code dst.length}</ul>
*/
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
if (dst == null) {
throw new NullPointerException("dst == null");
}
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(this, srcBegin);
}
if (srcEnd > length()) {
throw new StringIndexOutOfBoundsException(this, srcEnd);
}
int n = srcEnd - srcBegin;
if (srcEnd < srcBegin) {
throw new StringIndexOutOfBoundsException(this, srcBegin, n);
}
if (dstBegin < 0) {
throw new ArrayIndexOutOfBoundsException("dstBegin < 0. dstBegin=" + dstBegin);
}
// dstBegin can be equal to dst.length, but only in the case where zero chars are to be
// copied.
if (dstBegin > dst.length) {
throw new ArrayIndexOutOfBoundsException(
"dstBegin > dst.length. dstBegin=" + dstBegin + ", dst.length=" + dst.length);
}
if (n > dst.length - dstBegin) {
throw new ArrayIndexOutOfBoundsException(
"n > dst.length - dstBegin. n=" + n + ", dst.length=" + dst.length
+ "dstBegin=" + dstBegin);
}
getCharsNoCheck(srcBegin, srcEnd, dst, dstBegin);
}
/**
* Runs the test using the specified harness.
*
* @param harness the test harness (<code>null</code> not permitted).
*/
public void test(TestHarness harness)
{
ArrayIndexOutOfBoundsException object1 = new ArrayIndexOutOfBoundsException();
harness.check(object1 != null);
harness.check(object1.toString(), "java.lang.ArrayIndexOutOfBoundsException");
ArrayIndexOutOfBoundsException object2 = new ArrayIndexOutOfBoundsException("nothing happens");
harness.check(object2 != null);
harness.check(object2.toString(), "java.lang.ArrayIndexOutOfBoundsException: nothing happens");
ArrayIndexOutOfBoundsException object3 = new ArrayIndexOutOfBoundsException(null);
harness.check(object3 != null);
harness.check(object3.toString(), "java.lang.ArrayIndexOutOfBoundsException");
ArrayIndexOutOfBoundsException object4 = new ArrayIndexOutOfBoundsException(0);
harness.check(object4 != null);
harness.check(object4.toString(), "java.lang.ArrayIndexOutOfBoundsException: 0");
ArrayIndexOutOfBoundsException object5 = new ArrayIndexOutOfBoundsException(-1);
harness.check(object5 != null);
harness.check(object5.toString(), "java.lang.ArrayIndexOutOfBoundsException: -1");
ArrayIndexOutOfBoundsException object6 = new ArrayIndexOutOfBoundsException(Integer.MAX_VALUE);
harness.check(object6 != null);
harness.check(object6.toString(), "java.lang.ArrayIndexOutOfBoundsException: 2147483647");
}
/**
* Runs the test using the specified harness.
*
* @param harness the test harness (<code>null</code> not permitted).
*/
public void test(TestHarness harness)
{
// flag that is set when exception is caught
boolean caught = false;
try {
throw new ArrayIndexOutOfBoundsException("ArrayIndexOutOfBoundsException");
}
catch (ArrayIndexOutOfBoundsException e) {
// correct exception was caught
caught = true;
}
harness.check(caught);
}