add special implementation for add
The special implementation does not need to allocate an array for the varargs of addAll
This commit is contained in:
@@ -112,9 +112,10 @@ public class IntList {
|
|||||||
* the value to add
|
* the value to add
|
||||||
*/
|
*/
|
||||||
public void add(final int value) {
|
public void add(final int value) {
|
||||||
// TODO add a special implementation that does not
|
ensureCapacity(1);
|
||||||
// need to allocate an array for the varargs of addAll
|
|
||||||
addAll(value);
|
data[index] = value;
|
||||||
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -39,11 +39,9 @@ public class IntListTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAdd() {
|
public void testAdd() {
|
||||||
final IntList list = new IntList();
|
// setting initial size to one, so that the first add does not need to resize,
|
||||||
|
// but the second add must
|
||||||
list.addAll();
|
final IntList list = new IntList(1);
|
||||||
Assert.assertTrue(list.isEmpty());
|
|
||||||
Assert.assertEquals(0, list.size());
|
|
||||||
|
|
||||||
list.add(1);
|
list.add(1);
|
||||||
|
|
||||||
@@ -179,6 +177,10 @@ public class IntListTest {
|
|||||||
public void testAddArray() {
|
public void testAddArray() {
|
||||||
final IntList list = new IntList();
|
final IntList list = new IntList();
|
||||||
|
|
||||||
|
list.addAll();
|
||||||
|
Assert.assertTrue(list.isEmpty());
|
||||||
|
Assert.assertEquals(0, list.size());
|
||||||
|
|
||||||
final int size = 100;
|
final int size = 100;
|
||||||
final int[] ints = ThreadLocalRandom.current().ints(size).toArray();
|
final int[] ints = ThreadLocalRandom.current().ints(size).toArray();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user