add clear() method

This commit is contained in:
2017-12-03 09:19:13 +01:00
parent b7ef9d2e6f
commit ef5d7817b2
2 changed files with 22 additions and 1 deletions

View File

@@ -18,7 +18,6 @@ public final class IntList implements Serializable, Cloneable {
// TODO support Iterator
// TODO add mod counts
// TODO support sublists
// TODO use sorted flag in indexOf
// TODO add lastIndexOf
// TODO remove bounds checks that are handled by java, e.g. negative indices
// TODO clear
@@ -518,6 +517,18 @@ public final class IntList implements Serializable, Cloneable {
}
}
/**
* Removes all elements from this list.
* <p>
* The implementation is equivalent to calling {@code remove(0, size())} and
* {@code trim()}.
*/
public void clear() {
data = EMPTY_ARRAY;
size = 0;
sorted = true;
}
/**
* Returns a sequential {@link IntStream} with this collection as its source.
*

View File

@@ -447,6 +447,16 @@ public class IntListTest {
Assert.assertEquals(0, list.getCapacity());
}
@Test
public void testClear() {
final IntList list = IntList.of(2, 0, 1); // unsorted list
list.clear();
Assert.assertEquals(0, list.size());
Assert.assertEquals(0, list.getCapacity());
Assert.assertTrue(list.isSorted());
}
@Test
public void testHashCodeEquals() {
final IntList a = new IntList();