object Demo { def disp[A](a : Array[A]) : Unit = { for( i <- 0 to (a.length - 1)) println(a(i)) } def disp2[A](a : Array[A]) : Unit = { for( e <- a) println(e) } def main(args : Array[String]) { var a = Array("Chen" ,"Lin"); var a2 : Array[String] = new Array[String](2); var a3 = new Array[String](2); a2(0) = "Yanhua" a2(1) = "Feng" disp(a) disp2(a2) } }
object Demo { // display an 1d array def disp[A](a : Array[A]) : Unit = { for( e <- a) printf(s"$e "); println(); } // display an 2d array def disp2d[A](a : Array[Array[A]]) { for(row <- a) { for(column <- row) printf("%5d", column) println() } } def doubleNum(n : Int) : Int = 2*n; def main(args : Array[String]) { // apply, generate array var a = Array.apply("Chen" ,"Lin"); disp(a); // concatenate var b = Array(1, 2, 3, 4); var c = Array(5, 6, 7, 8); var d = Array.concat(b, c); disp(d); // copy var e = new Array[Int](4); Array.copy(d, 0, e, 0, 4); disp(e); // empty var f = Array.empty[Int] println(f.length); // iterate var g = Array.iterate[Int](10, 4)(doubleNum); disp(g) // 10, 20, 40, 80 // fill var h = Array.fill[Int](4)(1); disp(h) // 1 1 1 1 var i = Array.fill[Int](2, 3)(1); // 2*3 array disp2d(i) // ofDim var j = Array.ofDim[Int](2, 3) disp2d(j) // range var k = Array.range(1, 10) // 1, 2, 3, 4, 5, 6, 7, 8, 9 disp(k) // tabulate var l = Array.tabulate[Int](10)(doubleNum); disp(l) } }
object Demo { def disp[A](a : Array[A]) : Unit = { for (e <- a) printf(s"$e "); println(); } def main(args : Array[String]) { // create an array var a = Array(2, 1, 4, 3) var d = Array(5, 6, 7, 8) disp(a) println("Size: "+a.size) // + disp(10 +: a) // 10 2 1 4 3 disp(a :+ 10) // 2 1 4 3 10 // ++ disp(a ++ d) // 2 1 4 3 5 6 7 8 // slice var c = a.slice(0, 2) disp(c) // reverse, returns new mutable indexed sequence with elements in reversed order var b = a.reverse; disp(b) // clone var e = a.clone() e(0) = 100 // head, last println(a.head+" "+a.last) // indices println(a.indices) // sorted disp(a.sortWith(_.compareTo(_) < 0)) // 1, 2, 3, 4 } }