Scala深度学习1

day01

在scala中,变量类型统一用val表示,常量则用var表示,如下程序中,就会输出num1到4的变量类型

object BasicDaraType{
  def main(args: Array[String]): Unit = {
val num1 = 100 //Int
val num2 = 100L //Long
val num3 = 12.5F //Float
val num4 = 12.5D //Double
//=============================
val bool1 = true //Boolean
val bool2 = false //Boolean
//=============================
val c = 'a' //Char
val s = "abc" //String
println(num1.getClass.getSimpleName)
println(num2.getClass.getSimpleName)
println(num3.getClass.getSimpleName)
println(num4.getClass.getSimpleName)
 }
}

在scala中的变量也可以自动转换类型

//自动转换
val Value: Int = 12
val doubleValue: Double = Value
//强制类型转换
val intValue2: Int = doubleValue.toInt

在scala中实现输入操作

val br = new BufferedReader(new InputStreamReader(System.in))
val line = br.readLine()
println(line)

scala中的循环操作

while循环输出1-10

var count = 0
while (count<10){
  count+=1
  print(count)
}

do-while循环实现循环输入

var line = ""
do {
  val br = new BufferedReader(new InputStreamReader(System.in))
  line = br.readLine()
  println("line: " + line1)
}while(line != "")

for循环遍历数组

//单行
val arr = Array(1,2,3,4,5,6)
//loop
for (ele <- arr)println("element: " +ele)//for(ele <- 0to100)
arr.foreach(println)

//多行
val arr = Array(1,2,3,4,5,6)
//loop
for (ele <- arr1){
  println("element: " +ele)
}

Unit函数(类似java中的void无返回值)

def processLine(fileName: String,width: Int,line: String): Unit = {
  if (line.length > width)
    println("fileName: " + fileName)
}

变长参数(将函数定义为参数个数可变的形式)
.args.foreach(println _)能输出18和play basketball

def main(args: Array[String]): Unit = {
  getUserInfo(1001,"Mike","18","play basketball")
}

def getUserInfo(id: Int,userName: String,args: String*):Unit = {
  println("id: " + id)
  println("userName: " + userName)
  args.foreach(println _)
}

对象私有属性,可以进行实例成员隔离,非常重要
只能在类内部使用,对象都不能直接使用,伴生类也无法使用

 class User {
  private [this] val id:Int=1001
  private [com] val name:String="Mike"
  private  [web] val age:Int=20
}

scala中的if和else控制语句

//if/else控制语句
//单行
val fileName = "flume-ng.conf"
val result = if (fileName.endsWith(".conf")) true
else false

//多行
val fileName = "flume-ng.conf"
var result = ""
if (fileName.endsWith(".conf")){
  result = "flume-ng.conf"
} else{
  result = "default.conf"
}
println(result)

scala中的数组

//数组
val arr = new Array[Int](10)
//添加元素
arr(0) = 100
arr(1) = 200

arr.foreach(println)

scala中的map函数

def main(args: Array[String]): Unit = {
  import scala.collection.mutable.Map
  val map = Map("MAX"->100,"Mike"->90,"Bob"->85)
  //add element
  map("Jack")=88
  //update element
  map("Mike")=95
  //delete element
  map.remove("Jack")
  //print map
  println(map)
  //map迭代
  for (key<-map.keys)
    println(key)
  for (value<-map.values)
    println(value)
  for ((k,v)<-map)println(((k,v)))
}

scala中的元组

object TupleTest {
  def main(args: Array[String]): Unit = {
    val t = (1001,"Mike",18)
    println("id: " + t._1)
    println("name: " + t._2)
    println("age: " + t._3)
  }
}

match模式匹配(类似java中switch)

 //面向对象
val sex = "men"
sex match {
  case "men"=>println("I'm men")
  case "women"=>println("I'm women")
  case _=>println("I'm men")
}

头等函数(函数可以被当成参数传递,也可以被当成结果返回)

 //头等函数
def main(args: Array[String]): Unit = {
  var incr_num = (x:Int)=>x*x
  println(incr_num(10))
}
//多行语句头等函数
def main(args: Array[String]): Unit = {
  val print_val = (x:Int,y:Int)=>{
    println("x:"+x)
    println("y:"+y)
  }
  print_val(10,20)
}

占位符(只要每个参数在头等函数内只出现一次,可以把下划线当做一个或更多参数的占位符)

def main(args: Array[String]): Unit = {
  var list = List(1, 2, 3, 4, 5, 6)
  list = list.filter(_ >3)
  list.foreach(println _)
}

scala中的List操作

object LIst {
  def main(args: Array[String]): Unit = {
//创建一个List
val lst0 = List(1,7,9,8,0,3,5,4,6,2)
//将lst0中每个元素乘以10后生成一个新的集合
val ls2 = lst0.map(_*10)
println(ls2)//List(10,70,90,80,0,30,50,40,60,20)
//将lst0中的偶数取出来生成一个新的集合
val ls3 = lst0.filter(_%2==0)
println(ls3)//List(8,0,4,6,2)
//将lst0排序后生成一个新的集合
val ls4 = lst0.sortWith(_>_)
println(ls4)//List(9,8,7,6,5,4,3,2,1,0)
val ls5 = lst0.sortWith(_<_)
println(ls5)//List(0,1,2,3,4,5,6,7,8,9)

val ls6 = lst0.sorted
println(ls6)//List(0,1,2,3,4,5,6,7,8,9)

val aa = List("bob","mike","luck","jack").sortWith(_.length<_.length)
println(aa)//List(pear,apple,banana,orange)

//反转顺序
val res1 = lst0.reverse
println(res1)//List(2,6,4,5,3,0,8,9,7,1)

  }
}

附:价值一个亿的人工智能代码
Scala深度学习1_第1张图片

object TenBillion {
  def main(args: Array[String]): Unit = {
while (true){
  val br = new BufferedReader(new InputStreamReader(System.in))
  val line = br.readLine()
  println("robot:"+line.replace("吗?","!"))
}
  }
}

你可能感兴趣的:(Scala,大数据)