scala.collection.mutable

class ListBuffer

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

@serializable

final class ListBuffer[A]
extends Buffer[A] with TraversableClass[A, ListBuffer] with BufferTemplate[A, ListBuffer[A]] with Builder[A, List[A]] with SequenceForwarder[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
Method Summary
def +: (x : A) : ListBuffer[A]
Prepends a single element to this buffer. This operation takes constant time.
def += (x : A) : ListBuffer[A]
Appends 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.
def clear : Unit
Clears the buffer contents.
override def clone : ListBuffer[A]
Returns a clone of this buffer.
override def companion : Companion[ListBuffer]
The factory companion object that builds instances of class CC
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
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 : Sequence[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 SequenceForwarder
apply, 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, toSequence, toStream, mkString, addString, head, last, lastOption
Methods inherited from Builder
sizeHint, mapResult
Methods inherited from BufferTemplate
++:, ++:, append, appendAll, prepend, prependAll, prependAll, insert, trimStart, trimEnd, <<, ++=, +, +, ++, ++, -, -, --, --
Methods inherited from Shrinkable
-=, --=, --=
Methods inherited from Growable
+=, ++=, ++=
Methods inherited from Unhashable
hashCode, identityHashCode
Methods inherited from SequenceTemplate
size, zip, zipAll, zipWithIndex, indexWhere, findIndexOf, indexOf, lastIndexOf, lastIndexOf, lastIndexWhere, lastIndexWhere, reverse, reversedElements, startsWith, indexOfSeq, lastIndexOfSeq, lastIndexOfSeq, union, diff, intersect, removeDuplicates, patch, padTo, view, view, equals, toString, sortWith, findLastIndexOf, slice, equalsWith, containsSlice, projection
Methods inherited from PartialFunction
orElse, andThen
Methods inherited from Function1
compose
Methods inherited from IterableTemplate
elements, toIterable, takeRight, dropRight, first, firstOption, toSeq
Methods inherited from TraversableClass
newBuilder, genericBuilder, unzip, flatten, transpose
Methods inherited from TraversableTemplate
thisCollection, ++, ++, map, flatMap, filter, filterMap, filterNot, remove, partition, groupBy, /:, :\, headOption, tail, init, take, drop, slice, takeWhile, dropWhile, span, splitAt, copyToArray, toSet, mkString, mkString, addString, addString
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 : Companion[ListBuffer]
The factory companion object that builds instances of class CC
Overrides
Buffer.companion, TraversableClass.companion

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

override def length : Int
The current length of the buffer
Overrides
BufferTemplate.length, SequenceForwarder.length

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
BufferTemplate.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
BufferTemplate.+=, Builder.+=

def clear : Unit
Clears the buffer contents.
Overrides
BufferTemplate.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
BufferTemplate.+:

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
BufferTemplate.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
BufferTemplate.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
BufferTemplate.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
BufferTemplate.-=

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

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

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
BufferTemplate.stringPrefix