4. Scala array

Source: Internet
Author: User

1. Array

2, ArrayBuffer

3. Traversing array and Arraybuffer

4. Common Array Operations

1. Array

In Scala, array stands for a similar meaning to Java and is an immutable array of lengths. Since both Scala and Java are running in the JVM, the two sides can call each other, and the underlying implementation of the Scala array is actually a Java array.

  

2, ArrayBuffer

In Scala, if you need a variable-length collection class, you can use Arraybuffer. You need to import scala.collection.mutable.ArrayBuffer before use.

Create an empty arraybuffer:val B=arraybuffer[int] ()

Add an element using + = B+=1 b+= (2,3,4,5)

Add all elements from another collection B++=array (6,7,8)

Truncates the specified number of elements from the tail b.trimend (5)

Specify position Insert element B.insert (0,9,10)

Removes the element b.remove (1,3) from the specified position

Array and Arraybuffer convert each other B.toarray A.tobuffer

  

3. Traversing array and Arraybuffer

Using for loops and until traversal

  

Jump traversal

  

Trailing traverse

  

Using the enhanced for loop traversal

  

4. Common Array Operations

Array element summation A.sum

Get maximum value A.max

Sort an array scala.util.Sorting.quickSort (a)

Gets the contents of all elements in the array a.mkstring a.mkstring (",") a.mkstring ("(", ",", ")")

toString function a.tostring

  

4. Scala array

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.