Scala数组操作

数组基本操作

(1)

val s=Array("Hello","World")
s(0)="Goodbye" for(elem <- s)println(elem)

在Scala Worksheet里面的执行结果:

s: Array[String] = Array(Hello, World)
res0: Unit = ()
Goodbye
World
res1: Unit = ()

注:将字符串“Goodbye”赋值给数组s的第一个元素s(0),然后循环遍历 打印出来
Scala数组操作_第1张图片
(2)
注意要导入ArrayBuffer的包 : import scala.collection.mutable.ArrayBuffer

import scala.collection.mutable.ArrayBuffer

val b=ArrayBuffer[Int]()
b+=1
b+=(1,2,3,5)
b++=Array(8,13,21)

b.trimEnd(5)
b
b.insert(2,6)
b
b.insert(2,7,8,9)
b
b.remove(2)
b
b.remove(2,3)
b
b.toArray
b

变量b(ArrayBuffer)的可用函数
Scala数组操作_第2张图片

import scala.collection.mutable.ArrayBuffer

b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
res0: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1)
res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 5)
res2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 5, 8, 13, 21)

res3: Unit = ()
res4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)
res5: Unit = ()
res6: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 6, 2)
res7: Unit = ()
res8: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 7, 8, 9, 6, 2)
res9: Int = 7
res10: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 8, 9, 6, 2)
res11: Unit = ()
res12: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)
res13: Array[Int] = Array(1, 1, 2)
res14: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)

Scala数组操作_第3张图片
(3)

val a=new Array[String](10)
for(i <- 0 until a.length) println(i+":"+a(i))
a: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)
0:null
1:null
2:null
3:null
4:null
5:null
6:null
7:null
8:null
9:null
res0: Unit = ()

Scala数组操作_第4张图片
(4)

val c=Array(2,3,5,7,11)
val result=for(elem <- c) yield 2 * elem

for (elem <- c if elem % 2 ==0)yield 2 * elem
c.filter( _% 2 == 0).map(2*_)
c: Array[Int] = Array(2, 3, 5, 7, 11)
result: Array[Int] = Array(4, 6, 10, 14, 22)

res0: Array[Int] = Array(4)
res1: Array[Int] = Array(4)

这里写图片描述
(5)

import scala.collection.mutable.ArrayBuffer

Array(1,7,2,9).sum
ArrayBuffer("YiYou","had","a","little","lamb").max
import scala.collection.mutable.ArrayBuffer

res0: Int = 19
res1: String = little

注:sum求和;max求最大值(ArrayBuffer数组里面的元素为字符串,则比较字符串的长度)
这里写图片描述
(6)

import scala.collection.mutable.ArrayBuffer

val d=ArrayBuffer(1,7,2,9)
val bSorted=d.sorted
import scala.collection.mutable.ArrayBuffer

d: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 7, 2, 9)
bSorted: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 7, 9)

注:sorted排序
这里写图片描述
(7)

val e=Array(1,7,2,9)
scala.util.Sorting.quickSort(e)
e.mkString("and")
e.mkString("<",",",">")
e: Array[Int] = Array(1, 7, 2, 9)
res0: Unit = ()
res1: String = 1and2and7and9
res2: String = <1,2,7,9>

这里写图片描述
(8)
矩阵操作

val matrix=Array.ofDim[Double](3,4)
matrix(2)(1)=42

val triangle=new Array[Array[Int]](10)
for(i <- 0 until triangle.length) triangle(i)=new Array[Int](i+1)
matrix: Array[Array[Double]] = Array([D@193be75, [D@bd2fa1, [D@820f1a)
res0: Unit = ()

triangle: Array[Array[Int]] = Array(null, null, null, null, null, null, null, null, null, null) res1: Unit = ()

创建一个3行4列的矩阵,原子类型为Double
将值42赋值给第三行第二列的值
这里写图片描述

附件:完整代码

运行在:Scala Worksheet

import scala.collection.mutable.ArrayBuffer
val nums=new Array[Int](10)
val a=new Array[String](10)
for(i <- 0 until a.length) println(i+":"+a(i)) val s=Array("Hello","World") s(0)="Goodbye" for(elem <- s)println(elem) val b=ArrayBuffer[Int]() b+=1 b b+=(1,2,3,5) b b++=Array(8,13,21) b b.trimEnd(5) b b.insert(2,6) b b.insert(2,7,8,9) b b.remove(2) b b.remove(2,3) b b.toArray b import scala.collection.mutable.ArrayBuffer val c=Array(2,3,5,7,11) val result=for(elem <- c) yield 2 * elem for (elem <- c if elem % 2 ==0)yield 2 * elem c.filter( _% 2 == 0).map(2*_) Array(1,7,2,9).sum ArrayBuffer("YiYou","had","a","little","lamb").max val d=ArrayBuffer(1,7,2,9) val bSorted=d.sorted val e=Array(1,7,2,9) scala.util.Sorting.quickSort(e) e.mkString("and") e.mkString("<",",",">")
val matrix=Array.ofDim[Double](3,4)
matrix(2)(1)=42
val triangle=new Array[Array[Int]](10)
for(i <- 0 until triangle.length) triangle(i)=new Array[Int](i+1)
import scala.collection.mutable.ArrayBuffer
nums: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
a: Array[String] = Array(null, null, null, null, null, null, null, null, null, null)
0:null
1:null
2:null
3:null
4:null
5:null
6:null
7:null
8:null
9:null
res0: Unit = ()
s: Array[String] = Array(Hello, World)
res1: Unit = ()
Goodbye
World
res2: Unit = ()
b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1)
res4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1)
res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 5)
res6: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 5)
res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 5, 8, 13, 21)
res8: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 5, 8, 13, 21)
res9: Unit = ()
res10: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)
res11: Unit = ()
res12: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 6, 2)
res13: Unit = ()
res14: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 7, 8, 9, 6, 2)
res15: Int = 7
res16: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 8, 9, 6, 2)
res17: Unit = ()
res18: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)
res19: Array[Int] = Array(1, 1, 2)
res20: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)
import scala.collection.mutable.ArrayBuffer
c: Array[Int] = Array(2, 3, 5, 7, 11)
result: Array[Int] = Array(4, 6, 10, 14, 22)
res21: Array[Int] = Array(4)
res22: Array[Int] = Array(4)
res23: Int = 19
res24: String = little

d: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 7, 2, 9)
bSorted: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 7, 9)
e: Array[Int] = Array(1, 7, 2, 9)
res25: Unit = ()
res26: String = 1and2and7and9
res27: String = <1,2,7,9>
matrix: Array[Array[Double]] = Array([D@5409ae, [D@147a7b5, [D@1537b81)
res28: Unit = ()
triangle: Array[Array[Int]] = Array(null, null, null, null, null, null, null, null, null, null)
res29: Unit = ()

Scala数组操作_第5张图片

你可能感兴趣的:(scala)