diff --git a/jenetics/src/main/java/io/jenetics/util/Seq.java b/jenetics/src/main/java/io/jenetics/util/Seq.java index b5147498d9..bf36bccb07 100644 --- a/jenetics/src/main/java/io/jenetics/util/Seq.java +++ b/jenetics/src/main/java/io/jenetics/util/Seq.java @@ -577,7 +577,7 @@ public default Object[] toArray() { * @throws ArrayStoreException if the runtime type of the specified array is * not a super type of the runtime type of every element in this * array - * @throws NullPointerException if the given array is {@code null}. + * @throws NullPointerException if the given {@code array} is {@code null}. */ @SuppressWarnings("unchecked") public default B[] toArray(final B[] array) { @@ -602,6 +602,25 @@ public default B[] toArray(final B[] array) { return array; } + /** + * Returns an array containing the elements of this sequence, using the + * provided generator function to allocate the returned array. + * + * @since !__version__! + * + * @param generator a function which produces a new array of the desired + * type and the provided length + * @param the element type of the resulting array + * @return an array containing the elements in {@code this} sequence + * @throws ArrayStoreException if the runtime type of the specified array is + * not a super type of the runtime type of every element in this + * array + * @throws NullPointerException if the given {@code generator} is {@code null}. + */ + public default B[] toArray(final IntFunction generator) { + return toArray(generator.apply(length())); + } + /** * Returns a view of the portion of this sequence between the specified * {@code start}, inclusive, and {@code end}, exclusive. (If {@code start} diff --git a/jenetics/src/test/java/io/jenetics/util/SeqTest.java b/jenetics/src/test/java/io/jenetics/util/SeqTest.java index d36b33166c..a41bb2d837 100644 --- a/jenetics/src/test/java/io/jenetics/util/SeqTest.java +++ b/jenetics/src/test/java/io/jenetics/util/SeqTest.java @@ -184,4 +184,14 @@ public void toArray4() { final Integer[] array = mseq.toArray(new Integer[]{1, 2, 3, 4}); } + @Test + public void toArray5() { + final Seq mseq = MSeq.of("1", "2"); + final String[] array = mseq.toArray(String[]::new); + + Assert.assertEquals(array.length, 2); + Assert.assertEquals(array[0], mseq.get(0)); + Assert.assertEquals(array[1], mseq.get(1)); + } + }