Scala Array Operations

Source: Internet
Author: User

Array highlights If the length is fixed, array is used. If the length may change, arraybuffer is used. Do not use new when providing the initial value; Use () to access elements; Use for (ELEM <-Arr) to traverse elements. Use for (ELEM <-Arr if ...)... yield... to transform the original array into a new array. Scala arrays and Java Arrays can interoperate with each other, using arraybuffer and Scala. collection. the Conversion Function in javaconversions. Example:
Import Scala. collection. mutable. arraybuffer; object helloworld {def main (ARGs: array [String]) {// implement a variable array (Array Buffer ), the following two methods are equivalent to Val mutablearr = arraybuffer [int] (); Val mutablearr2 = new arraybuffer [int]; mutablearr + = 1; // use + = to add the element mutablearr + = (1, 2, 3, 4, 5) to the end. // Add multiple elements to the end, enclosed in parentheses mutablearr ++ = array (8, 9, 10) // you can use the ++ = Operator to append any set mutablearr. trimend (7) // remove the last 7 elements println ("length =" + mutablearr. length) println ("size =" + mutablearr. size) print ("element ="); For (ELEM <-mutablearr) {print (ELEM + ",")}}}
If you need to convert between array and arraybuffer, call the toarray () and tobuffer () methods respectively. Array Conversion The following uses the for derivation to generate a brand new array.
 
Object helloworld {def main (ARGs: array [String]) {Val a = array (2, 3, 5, 6); Val result = for (ELEM <-) yield ELEM * 2 result. copytoarray (a) For (ELEM <-a) {print (ELEM + ",") // output ,}}}
For (...) yield loops create a new set with the same type as the original set. If you start from the array, you get another array. If you start from the Array Buffer, in the (...) yield also gets an array buffer. You can use the following method to operate elements that meet certain conditions:
 
// Double the element with an even number and discard the element Val B =. filter (_ % 2 = 0 ). map (_ * 2); For (ELEM <-B) {print (ELEM + ",")}
Let's look at another example:
 
Import Scala. collection. mutable. arraybufferobject helloworld {def main (ARGs: array [String]) {// remove all negative numbers except the first negative number. Val a = arraybuffer (1,-2, 3, 5, -4,-5) var first = true // first collect the subscript to be retained. The returned result is a vector set Val indexs = for (I <-0 until. length if first | A (I)> = 0) yield {if (a (I) <0) First = false; I} print (indexs) // output: vector (0, 1, 2, 3) // then move the element to the expected position and cut the tail end for (j <-0 until indexs. length) {A (j) = a (indexs (j)} println (). trimend (. length-indexs. length) print ()}}
Multi-dimensional array
Object helloworld {def main (ARGs: array [String]) {Val matrix = array. ofdim [double] (3, 4) // construct a two-dimensional array matrix (0) (0) with three rows and four columns) = 56 // access the first element Val triangle = new array [array [int] (10) // create an array of ten rows, each row is an int-type array for (I <-0 until (triangle. length) {triangle (I) = new array [int] (I + 1) // convert each row into (I + 1) two-dimensional array of the column} // print the array for (I <-0 until (triangle. length) {for (j <-0 until (Triangle (I ). length) {print (Triangle (I) (j) + ",")} println ()}}}


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.