diff options
author | Martin Odersky <odersky@gmail.com> | 2006-03-13 11:11:59 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2006-03-13 11:11:59 +0000 |
commit | d3d7b7ce01ec8bace1dab775dccecacc07ca6182 (patch) | |
tree | 8780cd3facadbbb0e8cb926718c46163af68af3d /src | |
parent | 9e745473dca51f78ed4c10565b34213159fcf3b8 (diff) | |
download | scala-d3d7b7ce01ec8bace1dab775dccecacc07ca6182.tar.gz scala-d3d7b7ce01ec8bace1dab775dccecacc07ca6182.tar.bz2 scala-d3d7b7ce01ec8bace1dab775dccecacc07ca6182.zip |
added a toArray method to Seq.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/Iterator.scala | 3 | ||||
-rw-r--r-- | src/library/scala/List.scala | 13 | ||||
-rw-r--r-- | src/library/scala/Seq.scala | 15 |
3 files changed, 11 insertions, 20 deletions
diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala index ac394b2ce9..895a7eac92 100644 --- a/src/library/scala/Iterator.scala +++ b/src/library/scala/Iterator.scala @@ -427,7 +427,8 @@ trait Iterator[+A] { * * @param xs the array to fill. * @param start starting index. - * @return the given array <code>xs</code> filled with this list. + * @return the given array <code>xs</code> filled with the elements of this iterator. + * @pre The array must be large enough to hold all elements. */ def copyToArray[B >: A](xs: Array[B], start: Int): Array[B] = { var i = start; diff --git a/src/library/scala/List.scala b/src/library/scala/List.scala index a2ea568f38..824e109a60 100644 --- a/src/library/scala/List.scala +++ b/src/library/scala/List.scala @@ -415,19 +415,6 @@ sealed abstract class List[+a] extends Seq[a] { b.prependToList(this) } - /** Converts the list to an Array */ - def toArray[b >: a]: Array[b] = { - val xs = new Array[b](length) - var i = 0 - var these = this - while (!these.isEmpty) { - xs(i) = these.head - i = i + 1 - these = these.tail - } - xs - } - /** Reverse the given prefix and append the current list to that. * This function is equivalent to an application of <code>reverse</code> * on the prefix followed by a call to <code>:::</code>, but more diff --git a/src/library/scala/Seq.scala b/src/library/scala/Seq.scala index 2bb16f6f4c..14a9368522 100644 --- a/src/library/scala/Seq.scala +++ b/src/library/scala/Seq.scala @@ -150,6 +150,15 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Iterable[A] { } else Predef.error("cannot create subsequence"); + /** Transform this sequence into a list of all elements. + * + * @return a list which enumerates all elements of this sequence. + */ + def toList: List[A] = elements.toList; + + /** Converts this sequence to an Array */ + def toArray[B >: A]: Array[B] = elements.copyToArray(new Array[B](length), 0); + /** Fills the given array <code>xs</code> with the elements of * this sequence starting at position <code>start</code>. * @@ -160,12 +169,6 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Iterable[A] { def copyToArray[B >: A](xs: Array[B], start: Int): Array[B] = elements.copyToArray(xs, start); - /** Transform this sequence into a list of all elements. - * - * @return a list which enumerates all elements of this sequence. - */ - def toList: List[A] = elements.toList; - /** Customizes the <code>toString</code> method. * * @return a string representation of this sequence. |