scala.collection.mutable

class ListBuffer

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

@serializable

final class ListBuffer[A]
extends Buffer[A]
A Buffer implementation back up by a list. It provides constant time prepend and append. Most other operations are linear.
Author
Matthias Zenger
Version
1.0, 15/03/2004
Method Summary
def +: (x : A) : Buffer[A]
Prepends a single element to this buffer. It takes constant time.
override def += (x : A) : Unit
Appends a single element to this buffer. It takes constant time.
def - (x : A) : Buffer[A]
Removes a single element from the buffer and return the identity of the buffer. Same as this -= x; this. It takes linear time (except removing the first element, which is done in constant time).
override def -= (x : A) : Unit
Remove a single element from this buffer. It takes linear time (except removing the first element, which is done in constant time).
def apply (n : Int) : A
Returns the n-th element of this list. This method yields an error if the element does not exist. Takes time linear in the buffer size.
def clear : Unit
Clears the buffer contents.
override def clone : Buffer[A]
Returns a clone of this buffer.
override def elements : Iterator[A]
override def equals (obj : Any) : Boolean
Checks if two buffers are structurally identical.
def insertAll (n : Int, iter : Iterable[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 isEmpty : Boolean
Returns true if length == 0
def length : Int
Returns the length of this buffer. It takes linear time.
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
def remove (n : Int) : A
Removes the element on a given index position. Takes time linear in the buffer size (except for the first element, which is removed in constant time).
protected 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.
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 Buffer
+, ++=, ++=, ++=, ++, ++, ++, ++:, append, appendAll, prepend, prependAll, insert, trimStart, trimEnd, <<, hashCode
Methods inherited from Seq
lengthCompare, size, concat, last, lastOption, first, firstOption, headOption, isDefinedAt, lastIndexOf, findIndexOf, indexOf, map, flatMap, filter, take, drop, slice, slice, takeWhile, dropWhile, reverse, contains, subseq, toArray, toSeq, projection, equalsWith, startsWith, startsWith, endsWith, indexOf, containsSlice
Methods inherited from Collection
toString
Methods inherited from Iterable
partition, foreach, forall, exists, find, foldLeft, foldRight, /:, :\, reduceLeft, reduceRight, copyToBuffer, sameElements, toStream, mkString, mkString, mkString, addString, addString, addString, copyToArray, hasDefiniteSize
Methods inherited from PartialFunction
orElse, andThen
Methods inherited from Function1
compose
Methods inherited from AnyRef
getClass, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
def +:(x : A) : Buffer[A]
Prepends a single element to this buffer. It takes constant time.
Parameters
x - the element to prepend.
Returns
this buffer.
Overrides
Buffer.+:

override def +=(x : A) : Unit
Appends a single element to this buffer. It takes constant time.
Parameters
x - the element to append.
Overrides
Buffer.+=

def -(x : A) : Buffer[A]
Removes a single element from the buffer and return the identity of the buffer. Same as this -= x; this. It takes linear time (except removing the first element, which is done in constant time).
Parameters
x - the element to remove.
Returns
this buffer.

override def -=(x : A) : Unit
Remove a single element from this buffer. It takes linear time (except removing the first element, which is done in constant time).
Parameters
x - the element to remove.
Overrides
Buffer.-=

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.

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

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

def length : Int
Returns the length of this buffer. It takes linear time.
Returns
the length of this buffer.

override def isEmpty : Boolean
Returns true if length == 0

def apply(n : Int) : A
Returns the n-th element of this list. This method yields an error if the element does not exist. Takes time linear in the buffer size.
Parameters
n - the position of the element to be returned.
Returns
the n-th element of this buffer.
Throws
Predef.IndexOutOfBoundsException -

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
Buffer.update

def insertAll(n : Int, iter : Iterable[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
Buffer.insertAll

def remove(n : Int) : A
Removes the element on a given index position. Takes time linear in the buffer size (except for the first element, which is removed in constant time).
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
Buffer.remove

override def elements : Iterator[A]

Returns an iterator over all elements of this list.

Note: the iterator can be affected by insertions, updates and deletions that are performed afterwards on the buffer. To get iterator an over the current buffer snapshot, use toList.elements.
Throws
Predef.NoSuchElementException - if buffer is empty

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

override def equals(obj : Any) : Boolean
Checks if two buffers are structurally identical.
Returns
true, iff both buffers contain the same sequence of elements.

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