diff --git a/src/main/java/datastructures/DynamicArray.java b/src/main/java/datastructures/DynamicArray.java index 720f2d0..37fa80a 100644 --- a/src/main/java/datastructures/DynamicArray.java +++ b/src/main/java/datastructures/DynamicArray.java @@ -95,6 +95,11 @@ public void delete(int index) { data[j] = data[j + 1]; } size--; + + // Clear if last element in array + if (index == size) { + data[index] = null; + } } public boolean isEmpty() { diff --git a/src/test/java/datastructures/DynamicArrayTest.java b/src/test/java/datastructures/DynamicArrayTest.java index e53d99b..8ccbca6 100644 --- a/src/test/java/datastructures/DynamicArrayTest.java +++ b/src/test/java/datastructures/DynamicArrayTest.java @@ -47,6 +47,20 @@ public void Delete() { Assert.assertEquals("c", array.get(1)); } + @Test + public void DeleteLast() { + array.add("a"); + array.add("b"); + array.add("c"); + + array.delete(2); + + Assert.assertEquals(2, array.size()); + Assert.assertEquals("a", array.get(0)); + Assert.assertEquals("b", array.get(1)); + Assert.assertEquals(null, array.get(2)); + } + @Test public void isEmpty() { Assert.assertTrue(array.isEmpty());