Scala data structures (arrays, Map, and Tuple)

package com.zy
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

object test3 {
  def main(args: Array[String]): Unit = {
    //function1()

    //function2()



    //function3()

    //function4()

    //function5()

    //function6()


    //function8()

    //function9()

    function10()

  }

  //数组
  //不变长度数组(真数组)
  def function1():Unit={
    val ints=new Array[Int](20)
     // Assignment 
    INTS (. 3) = 999 for (I <- INTS) { 
      the println (I) 
    } 
    var ARR = the Array (. 1, "China", 9.9 )
     for (S <- ARR) { 
      the println (S) 
    } / / value (subscript) 
    the println (ARR (. 1 )) 
  } 
  DEF function2 (): Unit = { 
    Val ARR = Array.fill (. 3 (Math.random)) for (S <- ARR) { 
      the println (S) 
    } 
  } // variable length arrays 
  DEF Function3 (): Unit = { 
    Val Ab1 = new new

    

    


    


  ArrayBuffer [String] () 
    Val AB2 = ArrayBuffer [Int] (100, 200 ) 

    // ab1.insert (4, "China", "Japan") // Exception in the Thread "main" a java.lang.IndexOutOfBoundsException: 4 
    ab1.insert (0, "China", "Japan" )
     / * for (v <- ab1) { 
     println (v) 
    } * / 

    ab1 + = ( "America", "France") // append the symbol more simple 
    for (v < - Ab1) { 
      the println (V) 
    } 

    Ab1 ++ = the Array ( ". 1", ". 3", ". 6") // any additional set 
    println ( "--------------- ------- ")
     For (v <- ab1) { 
      the println (v)
    }

    ab1.insert ( . 1, "the Philippines") // in the first element behind 
    the println ( "----------------------" )
     for (V <- Ab1) { 
      the println (V) 
    } 

    the println ( "----------------------" )
     for (V <- Ab1 IF ! = V "Japan" ) { 
      the println (V) 
    } 

  } 

  DEF Function4 (): Unit = { 
    Val ARR = an ArrayBuffer [Int] (1,4,5,6,0 ) 
    Val RES = for (I <- 0 arr.length an until) the yield ARR (I ) * 2 
    println (RES) 

    // _ on behalf of each element in the array
     //arr.filter (_> 0) is determined is greater than the array elements of 0, returns an array
     // Map (_ * 2) multiplied by each element 2 
    arr.filter (_> 0) .map (_ * 2 ) .foreach (the println (_)) 

  } 

  DEF function5 (): Unit = { 

    var ARR = the Array (6,1,9,0,7 ) 
    the println (arr.max) 
    the println (arr.sum) 
    arr.sorted.foreach (the println ( _)) // ascending 
    the println ( "-----------" ) 

    //     _> _ compare two elements, a large discharge descending front 
    arr.sortWith (_> _). the foreach (the println ( _)) 
    the println ( "---------------" ) 
    arr.sortWith (_ > _). the foreach (the println (_)) // ascending

    the println (arr.mkString ( "And")) // delimiter 
    the println (arr.mkString ( "<", ",", ">")) // <6,1,9,0,7> // Specify prefix, separator, suffix 



  } 


  // multidimensional array 
  DEF function6 (): Unit = { 
    var array = new new the array [the array [Int]] (. 5 ) 
    array ( 0) = the array (1,4,5 ) 
    array ( . 1) the Array = (2,4,5 ) 
    Array ( 2) = the Array (6,4,5 )
     for (A <the -array IF ! A = null ; V <-a) { // double loop 
      the println (V) 
    } 

    println ("-------------------" ) 
    var arr2 is = the Array (the Array (l, 2,3), the Array (4,5,6), the Array (7, 8 ,. 9 ))
     for (A <-arr2; V <- A) { 
      the println (V) 
    } 



  } 

  DEF function7 (): Unit = { 
    val ARR = the Array (4,5,7 ) 

    // fixed length shift length 
    val = Buffer arr.toBuffer 
    buffer.foreach (the println (_)) 

    // longer turn fixed length 
    Val = AB2 an ArrayBuffer [Int] (100,200 ) 
   Val Array = ab2.toArray 

  } 


  // the Map 
  DEF function8 (): Unit = {
     / / immutable map (the value can not be changed)
    P = the Map var ( "AA" -> 12 is, "BB" -> 15, "CC" -> 28 )
     //   -> is used to create the tuple ( "aa", 12) ( "bb", 15) ( "CC", 28)
     // tuple :( "a", 1.99,33, " b") a set of data of any type [] 

    the println (p.get ( "AA")) // s Some (12 is) 
    the println (p.get ( "pppp")) // None 

    // variable map (variable value) 
    Val scala.collection.mutable.Map P2 = ( "XX" ->. 11, "YY" -> 22 is )
     // empty variable Map 
    Val hashMap '= new new mutable.HashMap [String, Int] 


// value 
    the println (p2.get ( "PP" )) 
    the println (P2.getOrElse ( "xx", 999)) // if the map contains the key "xx", returns the corresponding value; otherwise, return 999 


    // updates the value of the Map
    P2 ( "XX") = 100 
    the println (p2.get ( "XX" ))
     // append 
    P2 + = ( "G" ->. 1, "H" -> 2 ); 
    the println (P2) 
    // remove 
    p2- = ( "XX" ) 
    the println (P2) 


    // immutable mapping may be added 
    P + = ( "ZZ" -> 999, "mm" -> 888 ) 
    the println (P) 
    P - = ( "AA" ) 
    the println (P ) 


    // traverse Map 
    for ((K, V) <- P) { 
      the println ( "K:" + K + "\ T" + "V:" + V) 
    } 

    // traversal keys set 
    for (K <- P .( "K:" keySet) the println + K) 

    // iterate set of values 
    for(V <- p.values) the println ( "V:" + V) 

    the println ( "-------------------------" )
     // key 
    p.foreach (Me => the println (me._1))
     // value 
    p.foreach (Me => the println (me._2))
     // key-value pair 
    p.foreach (the println (_)) 

    the println ( "- ------------------------------ " )
     // inverted key-value pairs 
    for ((K, V) <- P) the println the yield (V, K) 

    var newMap = for ((K, V) <- P) the yield (V, K) 
    the println (newMap) 




  } 

  DEF function9 (): Unit = { 

    //Key will be sorted lexicographically 
    Val scala.collection.immutable.SortedSet mm = ( "F" ->. 9, "A" ->. 3, "W" ->. 8) // sorted lexicographically 
    println (mm) 

    months Val = scala.collection.mutable.LinkedHashMap ( "January" ->. 1, "February" -> 2, "March" ->. 3)     // create a sequence of the Map 
    months + = ( "Fourth" ->. 4 ) 
    the println ( "months =>" + months)    // months => the Map (January ->. 1, February -> 2, March ->. 3, Fourth ->. 4) 

  } 


  // tuple 
  DEF function10 (): Unit = { 
   
    TUP Val = (105, "China")
     // spaces in multiple splicing process will cause ambiguity 
    println (tup._1 + "\ t" + tup._2) 

    //Maximum Tuple22 Tuple3 tuple which has three elements 
    Val tup2 = new new Tuple3 [String, Int, Int] ( "AA", 11,44 ) 
    Val (A, B, C) = tup2 
    the println (A) 
    the println (B) 
    the println (C) 


    // grouped elements (_) is capitalized to distinguish whether 
    the println ( "NewAbcD" .partition (_. isupper)) // formed grouped tuples 

    // traverse 
    tup2.productIterator.foreach (the println (_)) / / easiest way, but not to splice "\ T" 
    the println () 
    tup2.productIterator.foreach (X => the println (X + "\ T" )) 

  } 

}

 

Guess you like

Origin www.cnblogs.com/qfdy123/p/11426519.html