标签:
1. 编写一段代码,将a设置为一个n个随机数的数组,要求随机数介于0(包含)和n(不包含)之间。
//import java.lang.Mathdef getArray(n:Int)={ val nums = new Array[Int](n) for(i <- 0 until nums.length){ nums(i) = (Math.random() * n).toInt } nums} getArray(10)//res18: Array[Int] = Array(5, 1, 6, 9, 0, 9, 6, 6, 8, 1)object App{ def main(args: Array[String]){ makeArr(10).foreach(println) } def makeArr(n:Int): Array[Int]={ val a = new Array[Int](n) val rand = new scala.util.Random() for(i <- a) yield rand.nextInt(n) }}object App{ def main(args: Array[String]){ val a = Array(1,2,3,4,5) exchangeArr(a).foreach(println) } def exchangeArr(a:Array[Int]):Array[Int]={ for(i <- 0 until a.length){ if(i % 2 == 0 && i != a.length -1){ val t = a(i) a(i) = a(i+1) a(i+1) = t } } a }}object App{ def main(args: Array[String]){ val a = Array(1,2,3,4,5) val b = revertYield(a) a.foreach(println) println b.foreach(println) } def revertYield(arr:Array[Int])={ for(i <- 0 until arr.length) yield { if(i % 2 == 0 && i != arr.length -1){ arr(i+1) }else if(i % 2 == 1 && i != arr.length -1){ arr(i-1) }else if(i % 2 == 0 && i == arr.length -1){ arr(i) }else{ arr(i-1) } } }}object App{ def main(args: Array[String]){ val a = Array(1,2,-3,-4,5,0) sortArr(a).foreach(println) } def sortArr(arr:Array[Int])={ val positiveIndexes = for(i <- 0 until arr.length if arr(i) >0 ) yield i val nonpositiveIndexes = for(i <- 0 until arr.length if arr(i) <= 0) yield i val newArr = new Array[Int](arr.length) for(i <- 0 until arr.length){ if(i < positiveIndexes.length) newArr(i) = arr(positiveIndexes(i)) else newArr(i) = arr(nonpositiveIndexes(i - positiveIndexes.length)) } newArr }}
object App{ def main(args: Array[String]){ val a = Array(1,2.0,-3,-4,5,0) println(average(a).toString) } def average(arr:Array[Double])={ arr.sum/arr.length }}//result: 0.16666666666666666 |
object App{ def main(args: Array[String]){ val a = Array(1,2,-3,-4,5) println(a.mkString("[",",","]")) println(reverseArray(a).mkString("[",",","]")) } def reverseArray(arr:Array[Int])={ for(i <- 0 until arr.length/2){ val t = arr(arr.length-1-i) arr(arr.length-1-i) = arr(i) arr(i) = t } arr }}/*result: [1,2,-3,-4,5][5,-4,-3,2,1]*/import scala.collection.mutable.ArrayBufferobject App{ def main(args: Array[String]){ val a = ArrayBuffer(1,2,-3,-4,5) println(a.mkString("[",",","]")) println(reverseArray(a).mkString("[",",","]")) } def reverseArray(arr:ArrayBuffer[Int])={ for(i <- 0 until arr.length/2){ val t = arr(arr.length-1-i) arr(arr.length-1-i) = arr(i) arr(i) = t } arr }}/*result: [1,2,-3,-4,5][5,-4,-3,2,1]*/object App{ def main(args: Array[String]){ val a = Array(6,2,2,6,7,5,2) println(a.mkString("[",",","]")) val b = a.distinct println(b.mkString("[",",","]")) }}/*result: [6,2,2,6,7,5,2][6,2,7,5]*/import scala.collection.mutable.ArrayBufferobject App{ def main(args: Array[String]){ var a = ArrayBuffer(1,-2,3,-4,5,-6,7) println(a.mkString("[",",","]")) var begin = System.currentTimeMillis() var b = ridAllNegativeButFirstA(a) var end = System.currentTimeMillis() println(end-begin) println(b.mkString("[",",","]")) println a = ArrayBuffer(1,-2,3,-4,5,-6,7) println(a.mkString("[",",","]")) begin = System.currentTimeMillis() b = ridAllNegativeButFirstB(a) end = System.currentTimeMillis() println(end-begin) println(b.mkString("[",",","]")) println a = ArrayBuffer(1,-2,3,-4,5,-6,7) println(a.mkString("[",",","]")) begin = System.currentTimeMillis() b = ridAllNegativeButFirstC(a) end = System.currentTimeMillis() println(end-begin) println(b.mkString("[",",","]")) } def ridAllNegativeButFirstA(a:ArrayBuffer[Int])={ var first = true var n = a.length var i = 0 while(i < n){ if(a(i) >= 0) i += 1 else{ if(first) { first = false; i += 1 } else { a.remove(i); n -= 1 } } } a } def ridAllNegativeButFirstB(a:ArrayBuffer[Int])={ var first = true val indexes = for(i <- 0 until a.length if first || a(i) >= 0) yield { if (a(i) < 0) first = false; i } for(j <- 0 until indexes.length) a(j) = a(indexes(j)) a.trimEnd(a.length - indexes.length) a } def ridAllNegativeButFirstC(a:ArrayBuffer[Int])={ val indexes = for(i <- 0 until a.length if a(i) < 0) yield i val seq = indexes.reverse.dropRight(1) for(i <- 0 until seq.length) a.remove(i) a } }/*result: [1,-2,3,-4,5,-6,7]5[1,-2,3,5,7][1,-2,3,-4,5,-6,7]4[1,-2,3,5,7][1,-2,3,-4,5,-6,7]3[-2,-4,5,-6,7]*/import scala.collection.mutable.ArrayBufferimport java.util.TimeZoneobject App{ def main(args: Array[String]){ val a = TimeZone.getAvailableIDs() val b = a.filter(_.startsWith("America/")) for(i <- 0 until b.length) b(i) = b(i).replace("America/","") b.foreach(println) }}/*result: AdakAnchorageAnguillaAntigua...*/ import scala.collection.JavaConversions.asScalaBufferimport scala.collection.mutable.Bufferimport java.awt.datatransfer._import java.util.TimeZoneobject App{ def main(args: Array[String]){ val flavors = SystemFlavorMap.getDefaultFlavorMap().asInstanceOf[SystemFlavorMap] val buffer = flavors.getNativesForFlavor(DataFlavor.imageFlavor) println(buffer) }}/*result[PNG, JFIF, DIB, ENHMETAFILE, METAFILEPICT]*/标签:
原文地址:http://www.cnblogs.com/chenjo/p/4436585.html