add toArray(int[])
This commit is contained in:
@@ -20,12 +20,13 @@ public final class IntList implements Serializable, Cloneable {
|
|||||||
// TODO support sublists
|
// TODO support sublists
|
||||||
// TODO add retainAll
|
// TODO add retainAll
|
||||||
// TODO add removeAll
|
// TODO add removeAll
|
||||||
|
// TODO add retainAll for sorted lists
|
||||||
|
// TODO add removeAll for sorted lists
|
||||||
// TODO add removeIf
|
// TODO add removeIf
|
||||||
// TODO add removeRange
|
// TODO add removeRange
|
||||||
// TODO add replace
|
// TODO add replace
|
||||||
// TODO add lastIndexOf
|
// TODO add lastIndexOf
|
||||||
// TODO add indexOf
|
// TODO add indexOf
|
||||||
// TODO add toArray(int[] a)
|
|
||||||
// TODO remove bounds checks that are handled by java, e.g. negative indices
|
// TODO remove bounds checks that are handled by java, e.g. negative indices
|
||||||
// TODO toString
|
// TODO toString
|
||||||
|
|
||||||
@@ -152,6 +153,7 @@ public final class IntList implements Serializable, Cloneable {
|
|||||||
|
|
||||||
ensureCapacity(values.length);
|
ensureCapacity(values.length);
|
||||||
|
|
||||||
|
// TODO do not copy the array twice
|
||||||
final int[] newData = new int[data.length];
|
final int[] newData = new int[data.length];
|
||||||
System.arraycopy(data, 0, newData, 0, pos);
|
System.arraycopy(data, 0, newData, 0, pos);
|
||||||
System.arraycopy(values, 0, newData, pos, values.length);
|
System.arraycopy(values, 0, newData, pos, values.length);
|
||||||
@@ -282,6 +284,15 @@ public final class IntList implements Serializable, Cloneable {
|
|||||||
return get(0, index);
|
return get(0, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int[] toArray(final int[] input) {
|
||||||
|
|
||||||
|
if (input.length < index) {
|
||||||
|
return toArray();
|
||||||
|
}
|
||||||
|
System.arraycopy(data, 0, input, 0, index);
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sorts the list into ascending order.
|
* Sorts the list into ascending order.
|
||||||
*/
|
*/
|
||||||
@@ -451,4 +462,5 @@ public final class IntList implements Serializable, Cloneable {
|
|||||||
throw new IllegalStateException(e);
|
throw new IllegalStateException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -285,6 +285,59 @@ public class IntListTest {
|
|||||||
Assert.assertArrayEquals(actual, new int[0]);
|
Assert.assertArrayEquals(actual, new int[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToArray() {
|
||||||
|
final IntList list = new IntList();
|
||||||
|
list.addAll(1, 2, 3, 4, 5, 6);
|
||||||
|
|
||||||
|
{
|
||||||
|
final int[] input = new int[1];
|
||||||
|
final int[] actual = list.toArray(input);
|
||||||
|
// input is too short -> new array returned
|
||||||
|
Assert.assertNotSame(input, actual);
|
||||||
|
Assert.assertArrayEquals(list.toArray(), actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
final int[] input = new int[list.size()];
|
||||||
|
final int[] actual = list.toArray(input);
|
||||||
|
// input fits exactly -> input returned
|
||||||
|
Assert.assertSame(input, actual);
|
||||||
|
Assert.assertArrayEquals(list.toArray(), actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
final int[] input = new int[list.size() + 1];
|
||||||
|
final int[] expected = { 1, 2, 3, 4, 5, 6, 0 };
|
||||||
|
final int[] actual = list.toArray(input);
|
||||||
|
// input too big -> input returned
|
||||||
|
Assert.assertSame(input, actual);
|
||||||
|
Assert.assertArrayEquals(expected, actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToArrayWithEmptyList() {
|
||||||
|
final IntList list = new IntList();
|
||||||
|
|
||||||
|
{
|
||||||
|
final int[] input = new int[0];
|
||||||
|
final int[] actual = list.toArray(input);
|
||||||
|
// input fits exactly -> input returned
|
||||||
|
Assert.assertSame(input, actual);
|
||||||
|
Assert.assertArrayEquals(list.toArray(), actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
final int[] input = new int[list.size() + 1];
|
||||||
|
final int[] expected = { 0 };
|
||||||
|
final int[] actual = list.toArray(input);
|
||||||
|
// input too big -> input returned
|
||||||
|
Assert.assertSame(input, actual);
|
||||||
|
Assert.assertArrayEquals(expected, actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRemove() {
|
public void testRemove() {
|
||||||
final IntList list = new IntList();
|
final IntList list = new IntList();
|
||||||
|
|||||||
Reference in New Issue
Block a user