1. 编写一段代码,将a设置为一个n个随机整数的数组,要求随机数介于0和n之间。
/**
* Created by Ibuki Suika on 2014/5/26.
*/
object ScalaApp {
def makeArr(n: Int): Array[Int] = {
val arr = new Array[Int](n)
val rand = new util.Random()
for (elem <- arr) yield rand.nextInt(n)
}
def main(args: Array[String]) {
makeArr(10).foreach(println)
}
}
/**
* Created by Ibuki Suika on 2014/5/26.
*/
object ScalaApp {
def exchange(a: Array[Int]) {
for (i <- 0 until(a.length - 1, 2)) {
val t = a(i)
a(i) = a(i + 1)
a(i + 1) = t
}
}
def main(args: Array[String]) {
val a = Array(1, 2, 3, 4, 5)
exchange(a)
a.foreach(println)
}
}
/**
* Created by Ibuki Suika on 2014/5/26.
*/
object ScalaApp {
def exchange(a: Array[Int]) = {
for (i <- 0 until a.length) yield {
if (i < a.length - 1 && i % 2 == 0) {
val t = a(i + 1)
a(i + 1) = a(i)
a(i) = t
}
a(i)
}
}
def main(args: Array[String]) {
val a = Array(1, 2, 3, 4, 5)
val b = exchange(a)
b.foreach(println)
}
}
/**
* Created by Ibuki Suika on 2014/5/26.
*/
import scala.collection.mutable.ArrayBuffer
object ScalaApp {
def exchange(a: Array[Int]) = {
val buf = new ArrayBuffer[Int]()
buf ++= (for (i <- a if i > 0) yield i)
buf ++= (for (i <- a if i == 0) yield i)
buf ++= (for (i <- a if i < 0) yield i)
buf.toArray
}
def main(args: Array[String]) {
val a = Array(1, 0, -1, 2, 3, -2, 0, 4, 5)
val b = exchange(a)
b.foreach(println)
}
}
/**
* Created by Ibuki Suika on 2014/5/26.
*/
object ScalaApp {
def main(args: Array[String]) {
val a = Array(1.0, 2.0, 3.0, 4.0)
val average = a.sum / a.length
println(average)
}
}
/**
* Created by Ibuki Suika on 2014/5/26.
*/
import scala.collection.mutable.ArrayBuffer
object ScalaApp {
def main(args: Array[String]) {
val a = Array(1, 2, 3, 4)
for (i <- 0 until a.length / 2) {
val t = a(i)
val index = a.length - i - 1
a(i) = a(index)
a(index) = t
}
a.foreach(println)
val b = new ArrayBuffer[Int]()
b ++= a.reverse
b.foreach(println)
}
}
/**
* Created by Ibuki Suika on 2014/5/26.
*/
object ScalaApp {
def main(args: Array[String]) {
val a = Array(1, 2, 3, 4, 4, 5, 5, 5)
val b = a.distinct
println(b.mkString(","))
}
}
8. 重新编写3.4节结尾的示例。收集负值元素的下标,反序,去掉最后一个下标,然后对每一个下标调用a.remove(i)。比较这样做的效率和3.4节中另外两种方法的效率。
/**
* Created by Ibuki Suika on 2014/5/26.
*/
import scala.collection.mutable.ArrayBuffer
object ScalaApp {
def main(args: Array[String]) {
val arr = ArrayBuffer(1, 2, -3, 4, -4, 5, 6, -4, -5)
val indexes = for (i <- 0 until arr.length if arr(i) < 0) yield i
val rindexes = indexes.reverse.dropRight(1)
for (index <- rindexes) arr.remove(index)
println(arr.mkString(","))
}
}
/**
* Created by Ibuki Suika on 2014/5/26.
*/
import scala.collection.mutable.ArrayBuffer
object ScalaApp {
def main(args: Array[String]) {
val ids = java.util.TimeZone.getAvailableIDs
val buf = ArrayBuffer[String]()
for (elem <- ids) {
if (elem.startsWith("America/")) {
val str = elem.drop(8)
buf += str
}
}
buf.sorted
buf.foreach(println)
}
}
/**
* Created by Ibuki Suika on 2014/5/26.
*/
import java.awt.datatransfer._
import scala.collection.JavaConversions.asScalaBuffer
import scala.collection.mutable.Buffer
object ScalaApp {
def main(args: Array[String]) {
val flavors = SystemFlavorMap.getDefaultFlavorMap().asInstanceOf[SystemFlavorMap]
val buf: Buffer[String] = flavors.getNativesForFlavor(DataFlavor.imageFlavor)
buf.foreach(println)
}
}