Studies related operations from zero array Scala (II), and maps the tuple

A: an array of related operations

Fixed-length arrays

        val array = Array [String] ( "a", "b", "c") // initialize the contents of the array directly
        println (array.mkString ( "|") )

 

        val array = new Array [String] (2) // new data of length 2 is performed by updating the assignment method
        array.update (0, "0")
        array.update (. 1, ". 1")
        the println (Array .mkString ( "|"))

Variable length arrays: an array of buffer

        scala.collection.mutable.ArrayBuffer Import
        Val an ArrayBuffer new new Data = [String] ();
        Data = .- ( ". 3") // single element deletion operation
        data .- = ( "3", "2") // single element deletion operation
        data. + = ( "1" ) // add a single element operation
        data. + = ( "2" , "3", "4") // more elements operating
        data. ++ = (Array ( "5", "6", "7")) // array operations variable constant array
        data. ++ = (data) // array of variable operation of the variable array
        data.trimEnd (2) // removes the last five elements
        data.remove (2) // remove the second set of bits a second element
        data.remove (2,3) // begin removing the second three elements
        val array = data.toArray [String] / / into an array

Through the array and the array buffer

        val array = Array [String] ( "a", "b", "c", "d") // into an array
        for (i <- array) // through each element
                println (i)


        for (i <- 0 until array.length ) // iterate length
                println (array.apply (i))

  

        for (i <- 0 until ( array.length, 2)) // two elements each jump
                println (array.apply (i))

        

        = the Array Array Val [String] ( "A", "B", "C", "D", "E", "F", "H")     
        for (I <- (0 an until be array.length) .reverse ) // reverse, but when prompted prompt IDE less than this method
                println (array.apply (i))

Conversion Array  

        = the Array Array Val [String] ( "A", "B", "C", "D", "E", "F", "H")
        Val for arrayb = (I <- (0 an until be array.length ) .reverse) yield array.apply (i) + "a" // array is an array of objects after the object yield, after the object is an array of buffer array buffer objects yield

        val array = Array[String]("a","b","c","d","e","f","h")
        val arrayB = array.map { x => x+"a" }.filter { x => x.equals("aa") }     //链式编程方法

Common method

        val array = Array [Int] ( 1,2,3,4) .sum // summation
        val array1 = Array [Int] ( 1,2,3,4) .max // maximum
        val array2 = Array [Int ] (1,2,3,4)
        array2.sortWith ((A, B) => A> B) .mkString ( "|") // Sort

scaladoc

        an ArrayBuffer array2 = Val [Int] (2,2,3,4)
        array2.append (. 6) // add a new element
        array2.appendAll (array2) // Add a new array
        array2.count (x => {if ( x = = 6) true else false}) // number that satisfies the calculated value
        . array2 + = (7) // add a new element
        val max = array2.max // maximum array
        println (array2.mkString ( "|") )
        val arrayBuffer = array2.padTo (15, 1234 ) // array into an array of fixed-length, not enough is filled by the second argument
        println (arrayBuffer.mkString ( "|") )

Multidimensional Arrays

        val array = Array.ofDim[String](2, 4) //两行四列
        array(1)(3) = "1234";
        println(array.mkString("|"))

Interoperability with Java

        import scala.collection.JavaConversions._ // want to convert java scala ways to use this
        // which contains a large collection of the scala convert java class

II: Mapping and tuples

Structure mapping

        val map = Map ( "aa" -> 4, "bb" -> 5, "cc" -> 6) // This method generates a set of fixed-length map
        val map1 = scala.collection.mutable.Map [String, int] () // this method generates a map of a set of variable length
        . MAP1 + = (( "dd",. 7))
        the println (map1.mkString ( "|"))

Gets the value map

        val map = Map ( "aa" -> 4, "bb" -> 5, "cc" -> 6) // This method generates a set of fixed-length map
        println (map.getOrElse ( "dd", "100" )) // get dd values, and if not return 100

Updates in the map

        val map1 = scala.collection.mutable.HashMap("aa"->4,"bb"->5,"cc"->6) //此方法生成一个定长的map集合
        map1.+=(("dd"->7)) //不可变map这个方法是返回新的map
        println(map1.mkString("|"))
        map1.-=("bb") //删除key
        println(map1.mkString("|"))
        map1.update("aa", 100) //不可变map没有这个方法
        println(map1.mkString("|"))

迭代映射

        val map1 = scala.collection.mutable.HashMap("aa"->4,"bb"->5,"cc"->6)
        for((key,value)<-map1){
                println(key +"|" + value)
        }
        println()
        for(key<-map1.keySet){
                println(map1.apply(key))
        }
        println()
        for((key,value)<-map1) yield(value,key) //反转map结构,key,value相互转换

已排序映射

        //生成不可变的的排序的map,如果必须要可变的话,建议用treemap
        val map1 = scala.collection.immutable.SortedMap("mm"->4,"aa"->5,"cc"->6)
        for((key,value)<-map1){
                println(key +"|" + value)
        }
        println()
        //生成可变的有顺序的map
        val map2 = scala.collection.mutable.LinkedHashMap("mm"->4,"aa"->5,"cc"->6)
        for((key,value)<-map2){
                println(key +"|" + value)
        }

与Java互操作

        //将一而终类型的变量转换为另一种类型的变量,这就是JavaConversions魅力
        //下面的例子就是将Properties转换为Map类型
        import scala.collection.JavaConversions.propertiesAsScalaMap
        val map:scala.collection.Map[String,String] = System.getProperties
        println(map.toString())

        //可用的还有
        import scala.collection.JavaConversions.mapAsScalaMap
        import scala.collection.JavaConversions.mapAsScalaConcurrentMap

元组

        //元组每个元素都可以是不同的类型
        val tt = ("aa",11,2.0f,5l)
        println(tt._1) //可以用_1,_2,_3,_4访问

拉链

        val aa = Array(1,2,3,4)
        val bb = Array(5,6,7,8)
        val cc = aa.zip(bb)//拉链操作
        println(cc.mkString("|"))

Guess you like

Origin www.cnblogs.com/wuxiaolong4/p/11827305.html