Scala Language Specification----Array class

Source: Internet
Author: User

Array class
The generic array class is defined as follows.
Final class Array[a] (Len:int) extends Seq[a] {
def length:int = Len
def apply (i:int): A = ...
def update (I:int, x:a): Unit = ...
def elements:iterator[a] = ...
def subarray (From:int, End:int): array[a] = ...
def filter (p:a = Boolean): array[a] = ...
def map[b] (f:a = B): array[b] = ...
def flatmap[b] (f:a = array[b]): array[b] = ...
}
If T is not a type parameter or abstract type, type Array[t] represents the native array type []t] in the host system. In this case, length returns the lengths of the array, apply represents the subscript, and update represents the element update. Due to the existence of syntax sugars for the Apply and update operations (§6.25), the following are the corresponding pairs of XS operations in Scala and java/c#:
Scala java/c#
Xs.length Xs.length
XS (i) xs[i]
XS (i) = e Xs[i] = E
Standard Reference class
The array also implements the sequence feature Scala. Seq, which defines the elements method to return a iterator that contains all the elements in the array
Because there are differences in the implementation of arrays in the parameterized type and in the host language in Scala, there are a few small differences to be aware of when working with arrays. Explained below.
First, unlike arrays in Java or C #, the array in Scala is not covariant, meaning that s<:t in Scala does not array[s] <: array[t]. However, if you can change s to T in the host environment, you can change the array of s into an array of T.
For example, array[string] is not consistent with array[object, even if the String is identical to Object. However, you can change an expression of type array[string] to Array[object]. The transition will be successful and will not throw classcastexception. Examples are as follows:
Val xs = new array[string] (2)
Val Ys:array[object] = XS/* * * * ERROR: Incompatible type
Val Ys:array[object] = Xs.asinstanceof[array[object]]//ok
Second, for a polymorphic array with a type parameter or abstract type T as an element type, its representation differs from []t. However, isinstanceof and asinstanceof still work as if the array array uses the standard representation of a single-state array.
val ss = new Array[string] (2)
def F[t] (xs:array[t]): array[string] =
if (xs.isinstanceof[array[string]]) xs.asinstanceof[array[string]
else throw new Error ("not an instance")
F (SS)//Return SS
The representation of polymorphic arrays also guarantees that the creation of polymorphic arrays is consistent with expectations. The following is an example of a Mkarray method that creates an array of any type T, given a sequence that defines the element and is of type T.
def Mkarray[t] (elems:seq[t]): array[t] = {
Val result = new Array[t] (elems.length)
Val I = 0
For (Elem <-elems) {
Result (i) = Elem
I + = 1
}
}
Note that under the type erase model of the Java array, the above method does not work as expected-in fact it will always return an object array.
Again, in the Java environment there is a method system.arraycopy, with two objects being parameters, specifying starting coordinates and lengths, copying elements from one object to another, and the element types of the two objects must be compatible. This method does not work correctly for Scala's polymorphic arrays because they have different representations. As a substitute for the adjoint object that should use the array class Array.copy method. The accompanying object also defines different array construction methods, and also defines the extraction method Unapplyseq (§8.1.7), which provides pattern matching on the array.
Package Scala
Scala Standard Library
Object Array {
/** copy elements from SRC to dest */
def copy (Src:anyref, Srcpos:int, Dest:anyref,
Destpos:int, length:int): Unit = ...
/** merges all the arrays in the parameter into one */
def Concat[t] (xs:array[t]*): array[t] = ...
/** create a contiguous array of integers */
def range (Start:int, end:int): array[int] = ...
/** creates an array with the given element */
def apply[a <: Anyref] (xs:a*): array[a] = ...
/** similar to above */
def apply (xs:boolean*): array[boolean] = ...
def apply (xs:byte*): array[byte] = ...
def apply (xs:short*): array[short] = ...
def apply (xs:char*): Array[char] = ...
def apply (xs:int*): array[int] = ...
def apply (xs:long*): array[long] = ...
def apply (xs:float*): array[float] = ...
def apply (xs:double*): array[double] = ...
def apply (xs:unit*): array[unit] = ...
/** creates an array that contains multiple copies of an element */
def Make[a] (N:int, elem:a): array[a] = ...
/** provides pattern matching on the array */
def Unapplyseq[a] (X:array[a]): option[seq[a]] = Some (x)
}
Example 12.3.1 the following method to copy the given array parameter and return the initial array and the copied array:
def Duplicate[t] (xs:array[t]) = {
Val ys = new Array[t] (xs.length)
Array.copy (XS, 0, ys, 0, Xs.length)
(XS, YS)
}

For more highlights, please follow: http://bbs.superwu.cn

Focus on Superman Academy QR Code: 650) this.width=650; "Src=" http://static.oschina.net/uploads/space/2015/0528/163017_IzIq_2273204.jpg " alt= "163017_iziq_2273204.jpg"/>

Follow the Superman college Java Free Learning Exchange Group:

650) this.width=650; "src=" Http://static.oschina.net/uploads/space/2015/0528/163017_2wui_2273204.png "alt=" 163017 _2wui_2273204.png "/>


Scala Language Specification----Array class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.