diff --git a/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java b/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java index cce62d9de..00c3e433f 100644 --- a/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java +++ b/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java @@ -298,14 +298,10 @@ public class ResizableDoubleArray implements DoubleArray, Serializable { * @param value to be added to end of array */ public synchronized void addElement(double value) { - numElements++; - if ((startIndex + numElements) > internalArray.length) { + if (internalArray.length <= startIndex + numElements) { expand(); } - internalArray[startIndex + (numElements - 1)] = value; - if (shouldContract()) { - contract(); - } + internalArray[startIndex + numElements++] = value; } /** diff --git a/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java b/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java index 972999458..fd85eb0b8 100644 --- a/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java +++ b/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java @@ -157,16 +157,16 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { da.addElement(2.0); da.addElement(4.0); da.addElement(6.0); - Assert.assertEquals(4, ((ResizableDoubleArray) da).getInternalLength()); + Assert.assertEquals(16, ((ResizableDoubleArray) da).getInternalLength()); Assert.assertEquals(3, da.getNumElements()); da.setElement(3, 7.0); - Assert.assertEquals(4, ((ResizableDoubleArray) da).getInternalLength()); + Assert.assertEquals(16, ((ResizableDoubleArray) da).getInternalLength()); Assert.assertEquals(4, da.getNumElements()); da.setElement(10, 10.0); - Assert.assertEquals(11, ((ResizableDoubleArray) da).getInternalLength()); + Assert.assertEquals(16, ((ResizableDoubleArray) da).getInternalLength()); Assert.assertEquals(11, da.getNumElements()); da.setElement(9, 10.0); - Assert.assertEquals(11, ((ResizableDoubleArray) da).getInternalLength()); + Assert.assertEquals(16, ((ResizableDoubleArray) da).getInternalLength()); Assert.assertEquals(11, da.getNumElements()); try {