1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| package scalatestkoinl import scala.collection.mutable.ArrayBuffer
object info { private var CONSTANT = "汪汪汪。。。" case class Person(name:String,age:Int) def main(args:Array[String]){ val arr1 = new Array[Int](8) println(arr1) val ab = ArrayBuffer[Int]() ab += 1 ab += (2,3,4,5) ab ++= Array(6,7) ab ++= ArrayBuffer(8,9) ab.insert(0,0) ab.remove(0) println(ab) var myArr = Array(1.2,2.4,3.6,4.8) for (x<-myArr){print(x+" ,")} println() var total = 0.0 for (i<- 0 to (myArr.length - 1)){total += myArr(i)} println("总和为:"+total) var max = myArr(0) for (i<-1 to(myArr.length-1)){if(myArr(i)>max)max=myArr(i)} println("最大值为:"+max)
val arr = Array(1,2,3,4,5,6,7,8,9) val newArr = for(e<- arr if e%2==0) yield e*5 println(newArr.toBuffer) val tuple=("itcast",3.14,65535) print(tuple._1)
val fruit = "apples"::("oranges"::("pears"::Nil)) val num = Nil println("head of fruit:"+fruit.head) println("tail of fruit:"+fruit.tail) println("check if fruit is empty:"+fruit.isEmpty) println("check if num is empty:"+num.isEmpty) println("take of fruit:"+fruit.take(2)) println("contains of fruit:"+fruit.contains("apples"))
val site = Set("Sougou","Google","Baidu") val nums : Set[Int] = Set() println("第一个网站:"+site.head) println("其余网站:"+site.tail) println("查看site集合是否为空:"+site.isEmpty) println("查看num集合是否为空:"+num.isEmpty) println("查看site的前2个网站:"+site.take(2)) println("查看集合是否包含网站Sougou:"+site.contains("Sougou")) val colors = Map("red"->"1","azure"->"2","peru"->"3") val perucolors = if(colors.contains("peru"))colors("peru") else 0 val azurecolors = colors.getOrElse("azure",0) println("获取colors中键为read的值:"+colors("red")) println("获取colors中所有的键:"+colors.keys) println("获取colors中所有的值:"+colors.values) println("获取colors中是否为空:"+colors.isEmpty) println("获取colors中是否包含键peru,包含则返回对应值,否则返回0:"+perucolors) println("获取colors中是否包含键azure,包含则返回对应值,否则返回0:"+azurecolors) val pt=new Point(10,20); pt.move(10,10) val loc = new Location(10,20,15); loc.move(10,10,5); SingletonObject.hello() val dog = new info dog.name = "二哈666" dog.printName() var people = new People people.speak() people.listen() people.run() println(matchTest(3)) def matchTest(x:Int):String = x match { case 1=>"one" case 2=>"two" case 3=>"many" } val alice = new Person("Alice", 25) val bob = new Person("Bob",32) val charlie = new Person("Charlie", 32) for (person<-List(alice,bob,charlie)){ person match { case Person("Alice",25) =>println("Hi Alice!") case Person("Bob",32)=>println("Hi Bob!") case Person(name,age)=> println("Name:"+name+"\t"+"Age:"+age) } } } }
class Point(val xc:Int,val yc:Int){ var x:Int = xc var y:Int = yc def move(dx:Int,dy:Int): Unit ={ x = x+dy y = y+dy println("x的坐标点:"+x); println("y的坐标点:"+y); } }
class Location(override val xc:Int, override val yc:Int,val zc:Int)extends Point(xc,yc){ var z:Int = zc def move(dx:Int,dy:Int,dz:Int): Unit ={ x = x+dx y = y+dy z = z+dz println("x的坐标点:"+x) println("y的坐标点:"+y) println("z的坐标点:"+z) } }
object SingletonObject{ def hello(): Unit ={println("Hello,This is Sinleton Object")} }
class info{ val id=666 private var name = "二哈" def printName():Unit=(println(info.CONSTANT+name)) }
trait Animal{ def speak() def listen():Unit={} def run():Unit={println("I am running")} }
class People extends Animal{ override def speak(): Unit = {println("I am speaking English")} }
|
评论区
欢迎你留下宝贵的意见,昵称输入QQ号会显示QQ头像哦~