From 236ad31fbd4c5214efb56df7b88cf2d1ebc6c5e5 Mon Sep 17 00:00:00 2001 From: Morgan James Delagrange Date: Tue, 19 Mar 2002 00:05:16 +0000 Subject: [PATCH] using the setArray(Object) method no longer produces an out of bounds exception git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130654 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/collections/ArrayIterator.java | 10 +++--- .../collections/TestArrayIterator2.java | 33 ++++++++++++++++--- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/commons/collections/ArrayIterator.java b/src/java/org/apache/commons/collections/ArrayIterator.java index 56ac3edef..1d3d49cbc 100644 --- a/src/java/org/apache/commons/collections/ArrayIterator.java +++ b/src/java/org/apache/commons/collections/ArrayIterator.java @@ -1,7 +1,7 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/ArrayIterator.java,v 1.9 2002/03/15 17:33:52 morgand Exp $ - * $Revision: 1.9 $ - * $Date: 2002/03/15 17:33:52 $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/ArrayIterator.java,v 1.10 2002/03/19 00:05:10 morgand Exp $ + * $Revision: 1.10 $ + * $Date: 2002/03/19 00:05:10 $ * * ==================================================================== * @@ -68,7 +68,7 @@ import java.util.NoSuchElementException; * * @author James Strachan * @author Mauricio S. Moura - * @version $Revision: 1.9 $ + * @version $Revision: 1.10 $ */ public class ArrayIterator implements Iterator { @@ -108,6 +108,6 @@ public class ArrayIterator implements Iterator { public void setArray( Object array ) { this.array = array; - this.index = -1; + this.index = 0; } } diff --git a/src/test/org/apache/commons/collections/TestArrayIterator2.java b/src/test/org/apache/commons/collections/TestArrayIterator2.java index bba33c9c5..82dfbd898 100644 --- a/src/test/org/apache/commons/collections/TestArrayIterator2.java +++ b/src/test/org/apache/commons/collections/TestArrayIterator2.java @@ -1,7 +1,7 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestArrayIterator2.java,v 1.2 2002/02/25 22:48:52 morgand Exp $ - * $Revision: 1.2 $ - * $Date: 2002/02/25 22:48:52 $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestArrayIterator2.java,v 1.3 2002/03/19 00:05:16 morgand Exp $ + * $Revision: 1.3 $ + * $Date: 2002/03/19 00:05:16 $ * * ==================================================================== * @@ -70,7 +70,7 @@ import java.util.NoSuchElementException; * * @author Morgan Delagrange * @author James Strachan - * @version $Id: TestArrayIterator2.java,v 1.2 2002/02/25 22:48:52 morgand Exp $ + * @version $Id: TestArrayIterator2.java,v 1.3 2002/03/19 00:05:16 morgand Exp $ */ public class TestArrayIterator2 extends TestIterator { @@ -119,5 +119,30 @@ public class TestArrayIterator2 extends TestIterator { e.getClass().equals((new NoSuchElementException()).getClass())); } } + + // proves that an ArrayIterator set with the constructor has the same number of elements + // as an ArrayIterator set with setArray(Object) + public void testSetArray() { + Iterator iter1 = new ArrayIterator(testArray); + int count1 = 0; + while (iter1.hasNext()) { + ++count1; + iter1.next(); + } + + assertEquals("the count should be right using the constructor", + count1,testArray.length); + + ArrayIterator iter2 = new ArrayIterator(); + iter2.setArray(testArray); + int count2 = 0; + while (iter2.hasNext()) { + ++count2; + iter2.next(); + } + + assertEquals("the count should be right using setArray(Object)", + count2,testArray.length); + } }