求和
print(Array(1, 7, 2, 9).sum)
结果
19
要使用sum方法,元素类型必须是数值类型:要么是整形,要么是浮点数或者BigInteger/BigDecimal。
print(ArrayBuffer("Mary", "had", "a", "little", "lamb").max)
结果
little
排序
val b = ArrayBuffer(1, 7, 2, 9) val bSorted = b.sorted print(bSorted)
结果
ArrayBuffer(1, 2, 7, 9)
排序
val a = Array(1, 7, 2, 9) scala.util.Sorting.quickSort(a) for (elem <- a) { print(elem + ",") }
结果
1,2,7,9,
数字间加了字符
val a=ArrayBuffer(1,7,2,9) val b= a.mkString(" and ") print(b)
结果
1 and 7 and 2 and 9
数字间加了字符
val a=ArrayBuffer(1,7,2,9) val c=a.mkString("<",",",">") print(c)
结果
<1,7,2,9>
概括
val d = Array(1, 7, 2, 9) val a = ArrayBuffer(1, 7, 2, 9) val b = a.mkString(" and ") val c = a.mkString("<", ",", ">") println(d.toString) println(a.toString) println(b.toString) println(c.toString)
结果
[I@a3cad7c ArrayBuffer(1, 7, 2, 9) 1 and 7 and 2 and 9 <1,7,2,9>
本文出自 “陈生龙” 博客,谢绝转载!