"Scala" Scala Array (iii)

Source: Internet
Author: User
Tags array definition

Basic content

1. Array definitions

Definition of fixed length, variable array, element addition, deletion, sorting, summation and other common operations

Import Scala.collection.mutable.ArrayBufferimport scala.math.bigdecimal.roundingmode/** * @author Xwolf * @date    2017-04-09 18:23 * @since 1.8 */object Collection {def main (arg:array[string]): Unit ={Multiplearray ()}/** * Array Creation */private def create (): unit={val a = new array[string] (1) A (0) = "Hello" println (A.tobuffer) Val A b =arraybuffer (3) ab+=1//append can accept multiple parameters, append elements at the end of the array ab.append (12,3,3) ab ++=array (5,12) println ("Insert Afte R 1: "+ AB" ab + = (3,5) println ("Insert after 2:" + AB)//Add Element Ab.insert (2,123,32) println at the specified index ("Remove B Efore: "+ab"//Remove elements of the current index Ab.remove (1) println ("Remove after 1:" + AB)//Remove the number of elements starting at the current index, exceeding the exception that would throw an subscript out of bounds ab.remove ( 4,3) println (ab) val ary = Ab.toarray val arrayb = Ary.tobuffer println (ARRAYB)}/** * Sort */def SOR    T () {val ary =arraybuffer[int] () ary ++=array (1,123,3,123,4,12,10) println (ary) val a = Ary.sortby (x=>x) println (a)//custom sort Val c= Ary.sortwith ((b) =>a.compareto (b) <0) println (c) Val d= Ary.sortwith ((b) =>a.compareto (b) >0) Val F = Ary.sorted println (f) println (d)//de-re-filter map val e = d.distinct.filter (_.% (    2)!=0). Map (_.+ (2)) println (e)}/** * Common operations */DEF calculator (): unit={val ary =array (12,2,12,33,2,123,1)  println (Ary.max)//maximum println (ary.min)//Minimum println (ary.sum)//Sum println (average (ary))//averaging}/** * averaging of the array    Rounding retains two decimal places * @param ary * @return * * Private def average (Ary:array[int]): bigdecimal={val len = ary.length    Val sum = ary.sum (bigdecimal.valueof (SUM)./(Bigdecimal.valueof (len))). Setscale (2,roundingmode.up)}/** * Multidimensional array */def Multiplearray (): Unit ={//multidimensional array definition val mary = new Array[array[int] [2] Mary (0) =array (1,2,3,1,4) Mary (1) =array (2,1,4) for (ary<-mary) {println (Ary.tobuffer)}//Definition 2 row 3 column array val nary =array.ofdim[int] (2,3    ) nary (0) (0) =12 nary (0) (1) =13 nary (0) (2) =14Nary (1) (0) =22 nary (1) (1) =23 nary (1) (2) =24 println (Nary.tobuffer) for (i<-0 until Nary.length) {println ( Nary (i). tobuffer)}}}

  

Implicit conversions are used in Scala array operations.

For more information, refer to:

Scala array

Scala-Implicit conversions

"Scala" Scala Array (iii)

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.