数组

三种声明方式

var z:Array[String] = new Array[String](3)
var z = new Array[String](3)
var z = Array("Runoob", "Baidu", "Google")
//通过索引访问每个元素


遍历方法

object Test {
def main(args: Array[String]) {
var myList = Array(1.9, 2.9, 3.4, 3.5)

// 输出所有数组元素
for ( x <- myList ) {
println( x )
}

// 计算数组所有元素的总和
var total = 0.0;
for ( i <- 0 to (myList.length - 1)) {
total += myList(i);
}
println("总和为 " + total);

// 查找数组中的最大元素
var max = myList(0);
for ( i <- 1 to (myList.length - 1) ) {
if (myList(i) > max) max = myList(i);
}
println("最大值为 " + max);

}
}


二维数组

import Array._

object Test {
def main(args: Array[String]) {
var myMatrix = ofDim[Int](3,3)

// 创建矩阵
for (i <- 0 to 2) {
for ( j <- 0 to 2) {
myMatrix(i)(j) = j;
}
}

// 打印二维阵列
for (i <- 0 to 2) {
for ( j <- 0 to 2) {
print(" " + myMatrix(i)(j));
}
println();
}

}
}


concat() 方法 合并数组

import Array._

object Test {
def main(args: Array[String]) {
var myList1 = Array(1.9, 2.9, 3.4, 3.5)
var myList2 = Array(8.9, 7.9, 0.4, 1.5)

var myList3 =  concat( myList1, myList2)

// 输出所有数组元素
for ( x <- myList3 ) {
println( x )
}
}
}


range() 方法 创建区间数组

import Array._

object Test {
def main(args: Array[String]) {
var myList1 = range(10, 20, 2)
var myList2 = range(10,20)

// 输出所有数组元素
for ( x <- myList1 ) {
print( " " + x )
}
println()
for ( x <- myList2 ) {
print( " " + x )
}
}
}



iterate

def iterate[T]( start: T, len: Int )( f: (T) => T ): Array[T]

scala> var b=Array.iterate(0,3)(a=>a+1)
b: Array[Int] = Array(0, 1, 2)

var (数组名)=Array.iterate((初始值),(数组长度))(计算函数，返回值为数组的元素)

scala> var b=Array.iterate(54,5)(a=>a-1)
b: Array[Int] = Array(54, 53, 52, 51, 50)


tabulate

def tabulate[T]( n: Int )(f: (Int)=> T): Array[T]

scala> var a = Array.tabulate(3)(x=>x+5)
a: Array[Int] = Array(5, 6, 7)

scala> var a = Array.tabulate(3)(x=>x+1)
a: Array[Int] = Array(1, 2, 3)

def tabulate[T]( n1: Int, n2: Int )( f: (Int, Int ) => T): Array[Array[T]]



fill

def fill[T]( n: Int )(elem: => T): Array[T]

def fill[T]( n1: Int, n2: Int )( elem: => T ): Array[Array[T]]

scala> var b = Array.fill(3)(4)
b: Array[Int] = Array(4, 4, 4)

scala> var b = Array.fill(3)(4*2)
b: Array[Int] = Array(8, 8, 8)

scala> var b = Array.fill(3,2)(4*2)
b: Array[Array[Int]] = Array(Array(8, 8), Array(8, 8), Array(8, 8))


ofDim

def ofDim[T]( n1: Int ): Array[T]

//创建指定长度数组
scala> var a =Array.ofDim[Int](1)
a: Array[Int] = Array(0)
//创建二维数组
scala> var a = Array.ofDim[Int](2,2)
a: Array[Array[Int]] = Array(Array(0, 0), Array(0, 0))

scala> a(0)(0)=1

scala> a
res1: Array[Array[Int]] = Array(Array(1, 0), Array(0, 0))
//创建三维数组
scala> var a =Array.ofDim[Int](3,3)
a: Array[Array[Int]] = Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0))


range

def range( start: Int, end: Int, step: Int ): Array[Int]

scala> var a = Array.range(1,10)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> var a = Array.range(1,10,2)
a: Array[Int] = Array(1, 3, 5, 7, 9)


元祖

Tuple1-22 最多22个

定义方法：

val t = (1, 3.14, "Fred")
val t = new Tuple3(1, 3.14, "Fred")



访问方法

object Test {
def main(args: Array[String]) {
val t = (4,3,2,1)

val sum = t._1 + t._2 + t._3 + t._4

println( "元素之和为: "  + sum )
}
}


productIterator（遍历输出）

object Test {
def main(args: Array[String]) {
val t = (4,3,2,1)

t.productIterator.foreach{ i =>println("Value = " + i )}
}
}


toString

object Test {
def main(args: Array[String]) {
val t = new Tuple3(1, "hello", Console)

println("连接后的字符串为: " + t.toString() )
}
}


swap

object Test {
def main(args: Array[String]) {
val t = new Tuple2("www.google.com", "www.runoob.com")

println("交换后的元组: " + t.swap )
}
}


06-24 4108

08-27 81
06-26 36