scala基础函数-03

package com.yjm.scala


object Demo04 {
  def main(args: Array[String]): Unit = {
    println(fun(1)(1))
    //字符串
    val str1 ="hello bjsxt"
    val str2 = "hello BJSXT"
    println(str1.indexOf("b"))
    
    //数组
    val array = new Array[Int](10)
    for(i<- 0 until array.length){
      array(i)=i*i
    }
    for(i<- 0 until array.length){
      println(array(i))
    }
    array.foreach(x=>println(x))
    
    Array.fill(5)("bjsxt")
    
    val arr2 = Array[String]("s1","s2","s3")
    arr2.map(x=> x+"~").foreach(x=>println(x))
    
    //集合
    val list = List(1,2,3,4)
    //Nil表示空list集合
    val list2 = 1::2::3::Nil
    list2.foreach(x=>println(x))
    for(elem <- list2){
      println(elem)
    }
    val filterList = list2.filter(x => x>2)
    filterList.foreach(x=>println(x))
    
    val nameList = List("范冰冰","迪丽热巴","林志玲")
    val count = nameList.count(x=>x.contains("bb"))
    println("满足条件个数:"+count)
    
    nameList
      //Builds a new collection by applying a function to all elements of this list. 
      .map(x=>"hello "+x)
      //Builds a new collection by applying a function to all elements of this list 
      //and using the elements of the resulting collections. 
      .flatMap(x=>x.split(" "))
      //Applies a function f to all elements of this list.
      .foreach(x=>println(x))
    
  }
  
  /**
   * 柯里化函数
   *     高阶函数的简化
   */
  def fun(num1:Int):Int => Int ={
    //匿名函数
    (num2:Int) => num1+num2
  }
  
  
  
}

 

posted @ 2018-09-06 22:11  华格瑞沙  阅读(133)  评论(0编辑  收藏  举报