Scala中Array的使用

2021-08-22 16:21:46 字數 1621 閱讀 8161

總共分為array和arraybuffer,這裡的操作引數很多,很難說的完整,這裡就說幾個常用的。建議先看部落格中的list的部分,因為array這一部分很多都是一樣的。

scala> val a=array(1,2,3)

a: array[int] = array(1, 2, 3)

scala> a:+(4)

res100: array[int] = array(1, 2, 3, 4)

scala> a.:+(4)

res101: array[int] = array(1, 2, 3, 4)

scala> a.+:(0)

res105: array[int] = array(0, 1, 2, 3)

scala> a++:array(4,5,6)

res104: array[int] = array(1, 2, 3, 4, 5, 6)

scala> a.drop(2)

res107: array[int] = array(3)

scala> a.dropright(2)

res108: array[int] = array(1)

scala> a.sorted

.reverse

res111: array[int] = array(3, 2, 1)

scala> val a=arraybuffer(1,2,3)

a: scala.collection

.mutable

.arraybuffer[int] = arraybuffer(1, 2, 3)

-尾端加元素

scala> a+=(4)

res112: a.type = arraybuffer

(1, 2, 3, 4)

scala> 0+:(a)

res113: scala.collection

.mutable

.arraybuffer[int] = arraybuffer(0, 1, 2, 3, 4)

scala> a.drop(2)

res114: scala.collection

.mutable

.arraybuffer[int] = arraybuffer(3, 4)

scala> a.dropright(2)

res116: scala.collection

.mutable

.arraybuffer[int] = arraybuffer(1, 2)

scala>

scala> a.sorted

res117: scala.collection

.mutable

.arraybuffer[int] = arraybuffer(1, 2, 3, 4)

scala>

其中關於array和arraybuffer的區別的話,還是一樣吧,都是效率問題

Scala中Set的使用

scala val a set 1,1,2,3 a scala.collection immutable set int set 1,2,3 scala a 4 res119 scala.collection immutable set int set 1,2,3,4 scala a list 5,...

scala中的Type使用

trait base case class s name string,age int extends base case class f name string,tel long extends base case class info t base b t,time string overrid...

Scala中for迴圈的使用

for i int 1 to 10 其實1 to 10 真正的寫法應該是 for i 1.to 10 range型的資料可以用by指定步長 for i 1 to 10 by 2 range型資料也可以倒序,但是要指定步長為 1 for i 10 to 1 by 1 通過reverse關鍵字倒序 fo...