scala.collection.mutable

class ListBuffer

[source: scala/collection/mutable/ListBuffer.scala]

@serializable

@SerialVersionUID(3419063961353022661L)

final class ListBuffer[A]
extends Buffer[A] with GenericTraversableTemplate[A, ListBuffer] with BufferLike[A, ListBuffer[A]] with Builder[A, List[A]] with SeqForwarder[A]
A Buffer implementation back up by a list. It provides constant time prepend and append. Most other operations are linear.
Author
Matthias Zenger
Martin Odersky
Version
2.8
Since
1
Method Summary
def += (x : A) : ListBuffer[A]
Appends a single element to this buffer. This operation takes constant time.
def +=: (x : A) : ListBuffer[A]
Prepends a single element to this buffer. This operation takes constant time.
override def -= (elem : A) : ListBuffer[A]
Remove a single element from this buffer. May take time linear in the buffer size.
override def apply (n : Int) : A
Return element at index `n`
def clear : Unit
Clears the buffer contents.
override def clone : ListBuffer[A]
Returns a clone of this buffer.
override def companion : GenericCompanion[ListBuffer]
The factory companion object that builds instances of class CC
override def equals (that : Any) : Boolean
The equality method defined in `AnyRef`.
def insertAll (n : Int, seq : Traversable[A]) : Unit
Inserts new elements at the index n. Opposed to method update, this method will not replace an element with a new one. Instead, it will insert a new element at index n.
override def iterator : Iterator[A]
Creates a new iterator over all elements contained in this iterable object.
override def length : Int
The current length of the buffer
def prependToList (xs : List[A]) : List[A]
Prepends the elements of this buffer to a given list
override def readOnly : List[A]
expose the underlying list but do not mark it as exported
override def remove (n : Int, count : Int) : Unit
Removes a given number of elements on a given index position. May take time linear in the buffer size.
def remove (n : Int) : A
Removes the element on a given index position. May take time linear in the buffer size
def result : List[A]
Returns collection resulting from this builder. The buffer's contents are undefined afterwards.
override def stringPrefix : java.lang.String
Defines the prefix of the string representation.
override def toList : List[A]
Converts this buffer to a list. Takes constant time. The buffer is copied lazily, the first time it is mutated.
protected def underlying : Seq[A]
The iterable object to which calls are forwarded
def update (n : Int, x : A) : Unit
Replaces element at index n with the new element newelem. Takes time linear in the buffer size. (except the first element, which is updated in constant time).
Methods inherited from SeqForwarder
isDefinedAt, lengthCompare, segmentLength, prefixLength, indexWhere, indexOf, reverseIterator, startsWith, endsWith, indexOfSeq, contains, indices
Methods inherited from IterableForwarder
sameElements
Methods inherited from TraversableForwarder
isEmpty, nonEmpty, hasDefiniteSize, foreach, forall, exists, count, find, foldLeft, foldRight, reduceLeft, reduceRight, reduceLeftOption, reduceRightOption, copyToBuffer, copyToArray, toArray, toSeq, toStream, mkString, addString, head, last, lastOption
Methods inherited from Builder
sizeHint, mapResult
Methods inherited from BufferLike
+:, ++=:, ++:, ++=:, ++:, append, appendAll, prepend, prependAll, prependAll, insert, trimStart, trimEnd, <<, ++=, +, +, ++, ++, -, -, --, --
Methods inherited from Shrinkable
-=, --=, --=
Methods inherited from Growable
+=, ++=, ++=
Methods inherited from SeqLike
thisCollection, toCollection, size, indexWhere, findIndexOf, indexOf, lastIndexOf, lastIndexOf, lastIndexWhere, lastIndexWhere, reverse, reverseMap, reversedElements, startsWith, indexOfSeq, lastIndexOfSeq, lastIndexOfSeq, union, diff, intersect, removeDuplicates, patch, updated, +:, :+, padTo, sortWith, sortWith, sortBy, view, view, hashCode, toString, findLastIndexOf, equalsWith, containsSlice, projection
Methods inherited from PartialFunction
orElse, andThen
Methods inherited from Function1
compose
Methods inherited from IterableLike
elements, toIterable, take, slice, takeWhile, takeRight, dropRight, zip, zipAll, zipWithIndex, canEqual, first, firstOption
Methods inherited from GenericTraversableTemplate
newBuilder, genericBuilder, unzip, flatten, transpose
Methods inherited from TraversableLike
repr, ++, ++, map, flatMap, filter, filterNot, partialMap, remove, partition, groupBy, /:, :\, sum, product, min, max, headOption, tail, init, drop, dropWhile, span, splitAt, copyToArray, toIndexedSeq, toSet, mkString, mkString, addString, addString, withFilter
Methods inherited from AnyRef
getClass, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
override def companion : GenericCompanion[ListBuffer]
The factory companion object that builds instances of class CC
Overrides
Buffer.companion, GenericTraversableTemplate.companion

protected def underlying : Seq[A]
The iterable object to which calls are forwarded
Overrides
SeqForwarder.underlying

override def length : Int
The current length of the buffer
Overrides
BufferLike.length, SeqForwarder.length

override def apply(n : Int) : A
Return element at index `n`
Throws
IndexOutofBoundsException - if the index is not valid
Overrides
BufferLike.apply, SeqForwarder.apply

def update(n : Int, x : A) : Unit
Replaces element at index n with the new element newelem. Takes time linear in the buffer size. (except the first element, which is updated in constant time).
Parameters
n - the index of the element to replace.
x - the new element.
Throws
Predef.IndexOutOfBoundsException - if n is out of bounds.
Overrides
BufferLike.update

def +=(x : A) : ListBuffer[A]
Appends a single element to this buffer. This operation takes constant time.
Parameters
x - the element to append.
Overrides
BufferLike.+=, Builder.+=

def clear : Unit
Clears the buffer contents.
Overrides
BufferLike.clear, Builder.clear

def +=:(x : A) : ListBuffer[A]
Prepends a single element to this buffer. This operation takes constant time.
Parameters
x - the element to prepend.
Returns
this buffer.
Overrides
BufferLike.+=:

def insertAll(n : Int, seq : Traversable[A]) : Unit
Inserts new elements at the index n. Opposed to method update, this method will not replace an element with a new one. Instead, it will insert a new element at index n.
Parameters
n - the index where a new element will be inserted.
iter - the iterable object providing all elements to insert.
Throws
Predef.IndexOutOfBoundsException - if n is out of bounds.
Overrides
BufferLike.insertAll

override def remove(n : Int, count : Int) : Unit
Removes a given number of elements on a given index position. May take time linear in the buffer size.
Parameters
n - the index which refers to the first element to remove.
count - the number of elements to remove.
Overrides
BufferLike.remove

def result : List[A]
Returns collection resulting from this builder. The buffer's contents are undefined afterwards.
Overrides
Builder.result

override def toList : List[A]
Converts this buffer to a list. Takes constant time. The buffer is copied lazily, the first time it is mutated.

def prependToList(xs : List[A]) : List[A]
Prepends the elements of this buffer to a given list
Parameters
xs - the list to which elements are prepended

def remove(n : Int) : A
Removes the element on a given index position. May take time linear in the buffer size
Parameters
n - the index which refers to the element to delete.
Returns
n the element that was formerly at position n.
Precondition
an element exists at position n
Throws
Predef.IndexOutOfBoundsException - if n is out of bounds.
Overrides
BufferLike.remove

override def -=(elem : A) : ListBuffer[A]
Remove a single element from this buffer. May take time linear in the buffer size.
Parameters
x - the element to remove.
Overrides
BufferLike.-=

override def iterator : Iterator[A]
Creates a new iterator over all elements contained in this iterable object.
Returns
the new iterator

override def readOnly : List[A]
expose the underlying list but do not mark it as exported
Overrides
BufferLike.readOnly

override def equals(that : Any) : Boolean
The equality method defined in `AnyRef`.

override def clone : ListBuffer[A]
Returns a clone of this buffer.
Returns
a ListBuffer with the same elements.

override def stringPrefix : java.lang.String
Defines the prefix of the string representation.
Returns
the string representation of this buffer.
Overrides
BufferLike.stringPrefix