[原创]Scala学习:数组的基本操作,数组进阶操作,多维数组

时间:2023-03-09 05:18:46
[原创]Scala学习:数组的基本操作,数组进阶操作,多维数组

1.Scala中提供了一种数据结构-数组,其中存储相同类型的元素的固定大小的连续集合。数组用于存储数据的集合,但它往往是更加有用认为数组作为相同类型的变量的集合

2 声明数组变量:

要使用的程序的数组,必须声明一个变量来引用数组,必须指定数组变量可以引用的类型。下面是语法声明数组变量:

var z:Array[String] = new Array[String](3)      or      var z = new Array[String](3)     or    var z = Array("Zara", "Nuha", "Ayan")

在这里,z被声明为字符串数组,最多可容纳三个元素。可以将值分配给独立的元素或可以访问单个元素,这是可以做到通过使用类似于以下命令:


z(0) = "Zara"; z(1) = "Nuha"; z(4/2) = "Ayan"

3.Scala中数组方法:

以下是重要的方法,可以同时使用数组。如上所示,则必须使用任何提及的方法之前,要导入Array._包。有关可用方法的完整列表,请Scala中的官方文件。


SN 方法及描述
1 def apply( x: T, xs: T* ): Array[T]
创建T对象,其中T可以是Unit, Double, Float, Long, Int, Char, Short, Byte, Boolean数组。
2 def concat[T]( xss: Array[T]* ): Array[T]
连接所有阵列成一个数组。
3 def copy( src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int ): Unit
复制一个数组到另一个。相当于Java的System.arraycopy(src, srcPos, dest, destPos, length).
4 def empty[T]: Array[T]
返回长度为0的数组
5 def iterate[T]( start: T, len: Int )( f: (T) => T ): Array[T]
返回一个包含一个函数的重复应用到初始值的数组。
6 def fill[T]( n: Int )(elem: => T): Array[T]
返回包含某些元素的计算的结果的次数的数组。
7 def fill[T]( n1: Int, n2: Int )( elem: => T ): Array[Array[T]]
返回一个二维数组,其中包含某些元素的计算的结果的次数。
8 def iterate[T]( start: T, len: Int)( f: (T) => T ): Array[T]
返回一个包含一个函数的重复应用到初始值的数组。
9 def ofDim[T]( n1: Int ): Array[T]
创建数组给出的尺寸。
10 def ofDim[T]( n1: Int, n2: Int ): Array[Array[T]]
创建了一个2维数组
11 def ofDim[T]( n1: Int, n2: Int, n3: Int ): Array[Array[Array[T]]]
创建3维数组
12 def range( start: Int, end: Int, step: Int ): Array[Int]
返回包含一些整数间隔等间隔值的数组。
13 def range( start: Int, end: Int ): Array[Int]
返回包含的范围内增加整数序列的数组。
14 def tabulate[T]( n: Int )(f: (Int)=> T): Array[T]
返回包含一个给定的函数的值超过从0开始的范围内的整数值的数组。
15 def tabulate[T]( n1: Int, n2: Int )( f: (Int, Int ) => T): Array[Array[T]]
返回一个包含给定函数的值超过整数值从0开始范围的二维数组。

 package first.scala

 import scala.collection.mutable.ArrayBuffer
import sun.org.mozilla.javascript.internal.ast.Yield object ScalaInAction {
//scala.Array /******************************************************************************************************************************/
//定长数组
//声明数组方式一:类型,大小
val nums = new Array[Int](10) //> nums : Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
val a = new Array[String](10) //> a : Array[String] = Array(null, null, null, null, null, null, null, null, n
//| ull, null)
//声明方式二:可以通过类型推断,推断出数组的类型
val s = Array("hello" , "world") //> s : Array[String] = Array(hello, world) s(0) = "goodbye" //可变数组
val b = ArrayBuffer[Int]() //> b : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() b += 1 //> res0: first.scala.ScalaInAction.b.type = ArrayBuffer(1)
b += (1,2,3,4) //> res1: first.scala.ScalaInAction.b.type = ArrayBuffer(1, 1, 2, 3, 4)
b ++= Array(12,15,63) //> res2: first.scala.ScalaInAction.b.type = ArrayBuffer(1, 1, 2, 3, 4, 12, 15,
//| 63)
//删除最后的2个元素
b.trimEnd(2)
b //> res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 4,
//| 12)
//给定索引处插入 数据
b.insert(2, 15)
b //> res4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 15, 2, 3
//| , 4, 12) //删除索引为2的元素
b.remove(2) //> res5: Int = 15 //转换为数组,类型的变换
b.toArray //> res6: Array[Int] = Array(1, 1, 2, 3, 4, 12)
b //> res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 4,
//| 12) /*******************************************************************************************************/
//数组的进阶操作 for(i <- 0 until a.length)
println(i + " : " + a(i)) //> 0 : null
//| 1 : null
//| 2 : null
//| 3 : null
//| 4 : null
//| 5 : null
//| 6 : null
//| 7 : null
//| 8 : null
//| 9 : null val c = Array(2,5,8,9,18) //> c : Array[Int] = Array(2, 5, 8, 9, 18)
val result = for(elem <- c) yield 2 * elem
//> result : Array[Int] = Array(4, 10, 16, 18, 36) //将c中的偶数乘2
for(elem <- c if elem % 2 == 0 ) yield 2 * elem
//> res8: Array[Int] = Array(4, 16, 36) //spark中方式,和上面的效果一样。先过滤后map
c.filter( _ % 2 == 0).map(2 * _) //> res9: Array[Int] = Array(4, 16, 36) //求和
Array(1,2,3).sum //> res10: Int = 6 //获取最长的字符串
ArrayBuffer("Mary", "had", "a", "little", "lamb").max
//> res11: String = little //排序,默认升序排序
val d = ArrayBuffer(1,7,2,9) //> d : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 7, 2, 9)
val bSorted = d.sorted //> bSorted : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 7,
//| 9) //快速排序
val e = Array(1,7,2,9) //> e : Array[Int] = Array(1, 7, 2, 9)
scala.util.Sorting.quickSort(e) //定义元素连接方式
e.mkString(" and ") //> res12: String = 1 and 2 and 7 and 9
//定义元素连接方式
a.mkString("<", "," , ">") //> res13: String = <null,null,null,null,null,null,null,null,null,null> /**************************************************************************************************************************/ //定义多维数组方法: Array.ofDim[Double](3,4)
val matrix = Array.ofDim[Double](3,4) //> matrix : Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0
//| , 0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0, 0.0))
matrix(2)(1) = 42 matrix //> res14: Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0, 0
//| .0, 0.0, 0.0), Array(0.0, 42.0, 0.0, 0.0))
val triangle = new Array[Array[Int]](10)
//> triangle : Array[Array[Int]] = Array(null, null, null, null, null, null, n
//| ull, null, null, null) for(i <- 0 until triangle.length)
triangle(i) = new Array[Int](i + 1)
triangle //> res15: Array[Array[Int]] = Array(Array(0), Array(0, 0), Array(0, 0, 0), Arr
//| ay(0, 0, 0, 0), Array(0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0,
//| 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0
//| , 0), Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) }