This class serves as a wrapper for Arrays with many of the operations found in indexed sequences. Where needed, instances of arrays are implicitly converted into this class. There is generally no reason to create an instance explicitly or use an ArrayOps type. It is better to work with plain Array types instead and rely on the implicit conversion to ArrayOps when calling a method (which does not actually allocate an instance of ArrayOps because it is a value class).
Neither Array nor ArrayOps are proper collection types (i.e. they do not extend Iterable or even IterableOnce). mutable.ArraySeq and immutable.ArraySeq serve this purpose.
The difference between this class and ArraySeqs is that calling transformer methods such as filter and map will yield an array, whereas an ArraySeq will remain an ArraySeq.
| Type parameters |
|
|---|---|
| Supertypes |
A copy of this array with an element appended.
A copy of this array with all elements of a collection appended.
A copy of this array with all elements of an array appended.
Builds a new array by applying a partial function to all elements of this array on which the function is defined.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | a new array resulting from applying the given partial function |
Finds the first element of the array for which the given partial function is defined, and applies the partial function to it.
Iterates over combinations. A _combination_ of length n is a subsequence of the original array, with the elements taken in order. Thus, Array("x", "y") and Array("y", "y") are both length-2 combinations of Array("x", "y", "y"), but Array("y", "x") is not. If there is more than one way to generate the same subsequence, only one will be returned.
For example, Array("x", "y", "y", "y") has three different ways to generate Array("x", "y") depending on whether the first, second, or third "y" is selected. However, since all are identical, only one will be chosen. Which of the three will be taken is an implementation detail that is not defined.
| Returns | An Iterator which traverses the possible n-element combinations of this array. |
|---|---|
| Example |
Array("a", "b", "b", "b", "c").combinations(2) == Iterator(Array(a, b), Array(a, c), Array(b, b), Array(b, c))
|
Tests whether this array contains a given value as an element.
| Value parameters |
|
|---|---|
| Returns |
|
Copy elements of this array to another array. Fills the given array xs starting at index 0. Copying will stop once either all the elements of this array have been copied, or the end of the array is reached.
| Type parameters |
|
|---|---|
| Value parameters |
|
Copy elements of this array to another array. Fills the given array xs starting at index start. Copying will stop once either all the elements of this array have been copied, or the end of the array is reached.
| Type parameters |
|
|---|---|
| Value parameters |
|
Copy elements of this array to another array. Fills the given array xs starting at index start with at most len values. Copying will stop once either all the elements of this array have been copied, or the end of the array is reached, or len elements have been copied.
| Type parameters |
|
|---|---|
| Value parameters |
|
Counts the number of elements in this array which satisfy a predicate
Computes the multiset difference between this array and another sequence.
| Value parameters |
|
|---|---|
| Returns | a new array which contains all elements of this array except some of occurrences of elements that also appear in |
Selects all the elements of this array ignoring the duplicates.
| Returns | a new array consisting of all the elements of this array without duplicates. |
|---|
Selects all the elements of this array ignoring the duplicates as determined by == after applying the transforming function f.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | a new array consisting of all the elements of this array without duplicates. |
The rest of the array without its n first elements.
The rest of the array without its n last elements.
Drops longest prefix of elements that satisfy a predicate.
| Value parameters |
|
|---|---|
| Returns | the longest suffix of this array whose first element does not satisfy the predicate |
Tests whether this array ends with the given array.
| Value parameters |
|
|---|---|
| Returns |
|
Tests whether this array ends with the given sequence.
| Value parameters |
|
|---|---|
| Returns |
|
Tests whether a predicate holds for at least one element of this array.
| Value parameters |
|
|---|---|
| Returns |
|
Selects all elements of this array which satisfy a predicate.
| Value parameters |
|
|---|---|
| Returns | a new array consisting of all elements of this array that satisfy the given predicate |
Selects all elements of this array which do not satisfy a predicate.
| Value parameters |
|
|---|---|
| Returns | a new array consisting of all elements of this array that do not satisfy the given predicate |
Finds the first element of the array satisfying a predicate, if any.
| Value parameters |
|
|---|---|
| Returns | an option value containing the first element in the array that satisfies |
Builds a new array by applying a function to all elements of this array and using the elements of the resulting collections.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | a new array resulting from applying the given collection-valued function |
Flattens a two-dimensional array by concatenating all its rows into a single array.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | An array obtained by concatenating rows of this array. |
Folds the elements of this array using the specified associative binary operator.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | the result of applying the fold operator |
Applies a binary operator to a start value and all elements of this array, going left to right.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns |
the result of inserting op(...op(z, x_1), x_2, ..., x_n) where |
Applies a binary operator to all elements of this array and a start value, going right to left.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns |
the result of inserting op(x_1, op(x_2, ... op(x_n, z)...)) where |
Tests whether a predicate holds for all elements of this array.
| Value parameters |
|
|---|---|
| Returns |
|
Apply f to each element for its side effects. Note: [U] parameter needed to help scalac's type inference.
Partitions this array into a map of arrays according to some discriminator function.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns |
A map from keys to arrays such that the following invariant holds: (xs groupBy f)(k) = xs filter (x => f(x) == k) That is, every key |
Partitions this array into a map of arrays according to a discriminator function key. Each element in a group is transformed into a value of type B using the value function.
It is equivalent to groupBy(key).mapValues(_.map(f)), but more efficient.
case class User(name: String, age: Int) def namesByAge(users: Array[User]): Map[Int, Array[String]] = users.groupMap(_.age)(_.name)
| Type parameters |
|
|---|---|
| Value parameters |
|
Partitions elements in fixed size arrays.
| Value parameters |
|
|---|---|
| Returns | An iterator producing arrays of size |
| See also | scala.collection.Iterator, method |
Selects the first element of this array.
| Returns | the first element of this array. |
|---|---|
| Throws |
|
Optionally selects the first element.
| Returns | the first element of this array if it is nonempty, |
|---|
Finds index of first occurrence of some value in this array after or at some start index.
| Value parameters |
|
|---|---|
| Returns | the index |
Finds index of the first element satisfying some predicate after or at some start index.
| Value parameters |
|
|---|---|
| Returns | the index |
Produces the range of all indices of this sequence.
| Returns | a |
|---|
The initial part of the array without its last element.
Iterates over the inits of this array. The first value will be this array and the final one will be an empty array, with the intervening values the results of successive applications of init.
| Returns | an iterator over all the inits of this array |
|---|
Computes the multiset intersection between this array and another sequence.
| Value parameters |
|
|---|---|
| Returns | a new array which contains all elements of this array which also appear in |
Tests whether the array is empty.
| Returns |
|
|---|
The size of this array.
| Returns | the number of elements in this array. |
|---|
Selects the last element.
| Returns | The last element of this array. |
|---|---|
| Throws |
|
Finds index of last occurrence of some value in this array before or at a given end index.
| Value parameters |
|
|---|---|
| Returns | the index |
Finds index of last element satisfying some predicate before or at given end index.
| Value parameters |
|
|---|---|
| Returns | the index |
Optionally selects the last element.
| Returns | the last element of this array$ if it is nonempty, |
|---|
Analogous to zip except that the elements in each collection are not consumed until a strict operation is invoked on the returned LazyZip2 decorator.
Calls to lazyZip can be chained to support higher arities (up to 4) without incurring the expense of constructing and deconstructing intermediary tuples.
val xs = List(1, 2, 3) val res = (xs lazyZip xs lazyZip xs lazyZip xs).map((a, b, c, d) => a + b + c + d) // res == List(4, 8, 12)
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | a decorator |
Compares the length of this array to a test value.
| Value parameters |
|
|---|---|
| Returns |
A value x < 0 if this.length < len x == 0 if this.length == len x > 0 if this.length > len |
Method mirroring SeqOps.lengthIs for consistency, except it returns an Int because length is known and comparison is constant-time.
These operations are equivalent to lengthCompare(Int), and allow the following more readable usages:
this.lengthIs < len // this.lengthCompare(len) < 0 this.lengthIs <= len // this.lengthCompare(len) <= 0 this.lengthIs == len // this.lengthCompare(len) == 0 this.lengthIs != len // this.lengthCompare(len) != 0 this.lengthIs >= len // this.lengthCompare(len) >= 0 this.lengthIs > len // this.lengthCompare(len) > 0
Builds a new array by applying a function to all elements of this array.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | a new array resulting from applying the given function |
Tests whether the array is not empty.
| Returns |
|
|---|
A copy of this array with an element value appended until a given target length is reached.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | a new array consisting of all elements of this array followed by the minimal number of occurrences of |
A pair of, first, all elements that satisfy predicate p and, second, all elements that do not.
Applies a function f to each element of the array and returns a pair of arrays: the first one made of those values returned by f that were wrapped in scala.util.Left, and the second one made of those wrapped in scala.util.Right.
Example:
val xs = Array(1, "one", 2, "two", 3, "three") partitionMap {
case i: Int => Left(i)
case s: String => Right(s)
}
// xs == (Array(1, 2, 3),
// Array(one, two, three))
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | a pair of arrays: the first one made of those values returned by |
Returns a copy of this array with patched values. Patching at negative indices is the same as patching starting at 0. Patching at indices at or larger than the length of the original array appends the patch to the end. If more values are replaced than actually exist, the excess is ignored.
| Value parameters |
|
|---|
Iterates over distinct permutations.
| Returns | An Iterator which traverses the distinct permutations of this array. |
|---|---|
| Example |
Array("a", "b", "b").permutations == Iterator(Array(a, b, b), Array(b, a, b), Array(b, b, a))
|
A copy of this array with an element prepended.
A copy of this array with all elements of a collection prepended.
A copy of this array with all elements of an array prepended.
Returns a new array with the elements in reversed order.
An iterator yielding elements in reversed order.
Note: xs.reverseIterator is the same as xs.reverse.iterator but implemented more efficiently.
| Returns | an iterator yielding the elements of this array in reversed order |
|---|
Computes a prefix scan of the elements of the array.
Note: The neutral element z may be applied more than once.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | a new array containing the prefix scan of the elements in this array |
Produces an array containing cumulative results of applying the binary operator going left to right.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns |
array with intermediate values. Example: Array(1, 2, 3, 4).scanLeft(0)(_ + _) == Array(0, 1, 3, 6, 10) |
Produces an array containing cumulative results of applying the binary operator going right to left.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns |
array with intermediate values. Example: Array(4, 3, 2, 1).scanRight(0)(_ + _) == Array(10, 6, 3, 1, 0) |
The size of this array.
| Returns | the number of elements in this array. |
|---|
Compares the size of this array to a test value.
| Value parameters |
|
|---|---|
| Returns |
A value x < 0 if this.size < otherSize x == 0 if this.size == otherSize x > 0 if this.size > otherSize |
Method mirroring SeqOps.sizeIs for consistency, except it returns an Int because size is known and comparison is constant-time.
These operations are equivalent to sizeCompare(Int), and allow the following more readable usages:
this.sizeIs < size // this.sizeCompare(size) < 0 this.sizeIs <= size // this.sizeCompare(size) <= 0 this.sizeIs == size // this.sizeCompare(size) == 0 this.sizeIs != size // this.sizeCompare(size) != 0 this.sizeIs >= size // this.sizeCompare(size) >= 0 this.sizeIs > size // this.sizeCompare(size) > 0
Selects an interval of elements. The returned array is made up of all elements x which satisfy the invariant:
from <= indexOf(x) < until
| Value parameters |
|
|---|---|
| Returns | an array containing the elements greater than or equal to index |
Groups elements in fixed size blocks by passing a "sliding window" over them (as opposed to partitioning them, as is done in grouped.)
| Value parameters |
|
|---|---|
| Returns | An iterator producing arrays of size |
| See also | scala.collection.Iterator, method |
Sorts this array according to the Ordering which results from transforming an implicitly given Ordering with a transformation function.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | an array consisting of the elements of this array sorted according to the ordering where |
| See also |
Sorts this array according to a comparison function.
The sort is stable. That is, elements that are equal (as determined by lt) appear in the same order in the sorted sequence as in the original.
| Value parameters |
|
|---|---|
| Returns | an array consisting of the elements of this array sorted according to the comparison function |
Sorts this array according to an Ordering.
The sort is stable. That is, elements that are equal (as determined by lt) appear in the same order in the sorted sequence as in the original.
| Value parameters |
|
|---|---|
| Returns | an array consisting of the elements of this array sorted according to the ordering |
| See also |
Splits this array into a prefix/suffix pair according to a predicate.
Note: c span p is equivalent to (but more efficient than) (c takeWhile p, c dropWhile p), provided the evaluation of the predicate p does not cause any side-effects.
| Value parameters |
|
|---|---|
| Returns | a pair consisting of the longest prefix of this array whose elements all satisfy |
Splits this array into two at a given position. Note: c splitAt n is equivalent to (c take n, c drop n).
| Value parameters |
|
|---|---|
| Returns | a pair of arrays consisting of the first |
Tests whether this array starts with the given array.
Tests whether this array contains the given array at a given index.
| Value parameters |
|
|---|---|
| Returns |
|
Tests whether this array contains the given sequence at a given index.
| Value parameters |
|
|---|---|
| Returns |
|
The rest of the array without its first element.
Iterates over the tails of this array. The first value will be this array and the final one will be an empty array, with the intervening values the results of successive applications of tail.
| Returns | an iterator over all the tails of this array |
|---|
An array containing the first n elements of this array.
An array containing the last n elements of this array.
Takes longest prefix of elements that satisfy a predicate.
| Value parameters |
|
|---|---|
| Returns | the longest prefix of this array whose elements all satisfy the predicate |
Create a copy of this array with the specified element type.
Transposes a two dimensional array.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | An array obtained by replacing elements of this arrays with rows the represent. |
Converts an array of pairs into an array of first elements and an array of second elements.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | a pair of Arrays, containing, respectively, the first and second half of each element pair of this Array. |
Converts an array of triples into three arrays, one containing the elements from each position of the triple.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | a triple of Arrays, containing, respectively, the first, second, and third elements from each element triple of this Array. |
A copy of this array with one single replaced element.
| Value parameters |
|
|---|---|
| Returns | a new array which is a copy of this array with the element at position |
| Throws |
|
Creates a non-strict filter of this array.
Note: the difference between c filter p and c withFilter p is that the former creates a new array, whereas the latter only restricts the domain of subsequent map, flatMap, foreach, and withFilter operations.
| Value parameters |
|
|---|---|
| Returns | an object of class |
Returns an array formed from this array and another iterable collection by combining corresponding elements in pairs. If one of the two collections is longer than the other, its remaining elements are ignored.
| Type parameters |
|
|---|---|
| Value parameters |
|
| Returns | a new array containing pairs consisting of corresponding elements of this array and |
Returns an array formed from this array and another iterable collection by combining corresponding elements in pairs. If one of the two collections is shorter than the other, placeholder elements are used to extend the shorter collection to the length of the longer.
| Value parameters |
|
|---|---|
| Returns | a new array containing pairs consisting of corresponding elements of this array and |
Zips this array with its indices.
| Returns | A new array containing pairs consisting of all elements of this array paired with their index. Indices start at |
|---|
© 2002-2022 EPFL, with contributions from Lightbend.
Licensed under the Apache License, Version 2.0.
https://scala-lang.org/api/3.2.0/scala/collection/ArrayOps.html