把scala list的幾種常見方法梳理彙總如下,日常開發場景基本上夠用了。
建立列表
建立空列表scala
>
val days
=list
("sunday"
,"monday"
,"tuesday"
,"wednesday"
,"thursday"
,"friday"
,"saturday"
)days
:list
[string]=
list
(sunday
,monday
,tuesday
,wednesday
,thursday
,friday
,saturday
)
用字串建立列表scala
>
val l
=nill:
scala
.collection
.immutable
.nil
.type
=list
()scala
>
val l
=list()l
:list
[nothing]=
list
()
用「:::」疊加建立新列表scala
>
val l
="hello"
::"hi"
::"hah"
::"wow"
::"woow"
::nill:
list
[string]=
list
(hello,hi
,hah
,wow
,woow
)
通過索引獲取列表值scala
>
val wow =l
:::list
("wooow"
,"woooow"
)wow
:list
[string]=
list
(hello,hi
,hah
,wow
,woow
,wooow
,woooow
)
獲取值長度為3的元素數目scala
>l(
3)res0
:string
=wow
返回去掉l頭兩個元素的新列表scala
>l.
count(s
=>s.
length ==3
)res1
:int
=2
返回去掉l後兩個元素的新列表scala
>l.
drop(2
)res2
:list
[string]=
list
(hah
,wow
,woow
)scala
>
lres3
:list
[string]=
list
(hello,hi
,hah
,wow
,woow
)
判斷l是否存在某個元素scala
>l.
dropright(2
)res5
:list
[string]=
list
(hello,hi
,hah
)scala
>
lres6
:list
[string]=
list
(hello,hi
,hah
,wow
,woow
)
濾出長度為3的元素scala
>l.
exists(s
=>
s ==
"hah"
)res7
:boolean
=true
判斷所有元素是否以「h」打頭scala
>l.
filter(s
=>s.
length ==3
)res8
:list
[string]=
list
(hah
,wow
)
判斷所有元素是否以「h」結尾scala
>l.
forall(s
=>s.
startswith
("h"
))res10
:boolean
=false
列印每個元素scala
>l.
forall(s
=>s.
endswith
("w"
))res11
:boolean
=false
取出第乙個元素scala
>l.
foreach(s
=>
print(s
+' '
))hello
hihah
wow woow
取出最後乙個元素scala
>l.
head
res17
:string
=hello
剔除最後乙個元素,生成新列表scala
>l.
last
res20
:string
=woow
剔除第乙個元素,生成新列表scala
>l.
init
res18
:list
[string]=
list
(hello,hi
,hah
,wow
)
判斷列表是否為空scala
>l.
tail
res49
:list
[string]=
list(hi
,hah
,wow
,woow
)
獲得列表長度scala
>l.
isempty
res19
:boolean
=false
修改每個元素,再反轉每個元素形成新列表scala
>l.
length
res21
:int
=5
生成用逗號隔開的字串scala
>l.
map(
s =>
)res29
:list
[string]=
list(10
-olleh,10
-ih,10
-hah,10
-wow,10
-woow
)
反序生成新列表scala
>l.
mkstring
(", "
)res30
:string
=hello,hi
,hah
,wow
,woow
按字母遞增排序scala
>l.
reverse
res41
:list
[string]=
list
(woow
,wow
,hah,hi
,hello
)
scala
>l.
sortwith(_
.compareto(_
)<0)
res48
:list
[string]=
list
(hah
,hello,hi
,woow
,wow
)
陣列 的幾種常見方法
unshift 新增,將要給定的數值新增到陣列的開頭 var arr 西瓜 蘋果 雪梨 香蕉 arr.unshift 桃子 哈密瓜 桃子 哈密瓜 西瓜 蘋果 雪梨 香蕉 console.log arr shift 方法 從陣列中刪除第乙個元素,並返回該元素的值。此方法更改陣列的長度。從陣列中刪除的元...
隱藏div的幾種常見方法
1.display none 2.visibility hidden 3.background color transparent 或者設成與背景一樣的顏色即可 4.opacity來設定不透明級別,注意相容性filter 5.給div乙個margin負值,這個負值恰好等於div自身的高度或寬度 6....
跨域幾種常見方案
一 jsonp 客戶端 伺服器端 echo request callback json encode array res 1111 msg 請求成功 二 jquery 客戶端 服務端 echo request callback json encode array res 1111 msg 請求成功 ...