/** * EX 1 : * Write a code snippet that sets a to an array of n random integers between 0 (inclusive) and n (exclusive). * * @param n * @return */ privatedefrandArray(n: Int): Array[Int] = { val a = newArray[Int](n) for (i <- a.indices) { a(i) = Random.nextInt(n) } a }
/** * Ex 3: * * Repeat the preceding assignment, but produce a new array with the swapped values. Use for/yield. * * @param arr * @return */ privatedefforyieldArray(arr: Array[Int]) = { for (i <- arr.indices) yieldif ((i & 0x1) == 0) if (i + 1 < arr.length) { arr(i + 1) } else { arr(i) } else { arr(i - 1) } }
/** * Ex 3: * * Repeat the preceding assignment, but produce a new array with the swapped values. Use for/yield. * * @param arr * @return */ privatedefforyieldArray2(arr: Array[Int]) = { for (i <- 0 until arr.length) yield arr(if (i % 2 == 0) { // be aware for yield loop , there is no `{` after // `for`, otherwise it is syntax error if (i + 1 < arr.length) { i + 1 } else { i } } else { i - 1 }) }
/** * Ex 4: * Given an array of integers, produce a new array that contains all positive values of the original array, in * their original order, followed by all values that are zero or negative, in their original order. * * @param arr * @return */ privatedefreArrangeArray(arr: Array[Int]): Array[Int] = { val b = newArray[Int](arr.size) var idx = 0 for (i <- arr.indices if arr(i) > 0) { b(idx) = arr(i) idx += 1 } for (i <- arr.indices if arr(i) <= 0) { b(idx) = arr(i) idx += 1 } b }
/** * Ex 4: * Given an array of integers, produce a new array that contains all positive values of the original array, in * their original order, followed by all values that are zero or negative, in their original order. * * @param arr * @return */ privatedefreArrangeArray2(arr: Array[Int]): Array[Int] = { arr.filter(_ > 0) ++ arr.filter(_ <= 0) }
ex 5
1 2 3 4 5 6 7 8
/** * Ex 5 : * How do you compute the average of an Array[Double]? * * @param arr * @return */ privatedefcalcAverage(arr: Array[Double]): Double = arr.sum / arr.size
/** * Ex 6 : * How do you rearrange the elements of an Array[Int] so that they appear in * reverse sorted order? How do you do the same with an ArrayBuffer[Int]? * * @param arr * @return */ privatedefreverseSortArray(arr: Array[Int]): Array[Int] = arr.sorted(Ordering.Int.reverse)
/** * Ex 6 : * How do you rearrange the elements of an Array[Int] so that they appear in * reverse sorted order? How do you do the same with an ArrayBuffer[Int]? * * @param arr * @return */ privatedefreverseSortArray2(arr: Array[Int]): Array[Int] = arr.sortWith(_ > _)
/** * * Ex 6 : * reverse and sort an Arraybuf * * @param buf * @return */ privatedefreverseSortArrayBuf(buf: ArrayBuffer[Int]): ArrayBuffer[Int] = { val arr = reverseSortArray(buf.toArray) buf.clear() arr.copyToBuffer(buf) buf }
/** * Ex 7 : * Write a code snippet that produces all values from an array with duplicates * removed. (Hint: Look at Scaladoc.) * * @param arr * @return */ privatedefgetDistinctArray(arr: Array[Int]): Array[Int] = arr.distinct
/** * EX 8 : * * Suppose you are given an array buffer of integers and want to remove all but the first negative number. Here is * a sequential solution that sets a flag when the first negative number is called, then removes all elements beyond. * * this method is wrong because it changes the postion of the original array * * @param arr * @return */ privatedefkeepFirstNegative(arr: ArrayBuffer[Int]): ArrayBuffer[Int] = { val neg = arr.filter(_ < 0) arr.filter(_ > 0) ++ neg.dropRight(neg.size - 1)
}
/** * EX 8 : * * Suppose you are given an array buffer of integers and want to remove all but the first negative number. Here is * a sequential solution that sets a flag when the first negative number is called, then removes all elements beyond. * * this method is wrong because it changes the postion of the original array * * @param arr * @return */ privatedefkeepFirstNegative2(arr: ArrayBuffer[Int]): ArrayBuffer[Int] = { val indexes = for (i <- arr.indices if arr(i) < 0) yield i val a = indexes.drop(1) for (i <- a.indices.reverse) { // notice you need to reverse it when drop arr.remove(a(i)) } arr }
ex 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * Ex 10: * * Make a collection of all timezones returned by java.util.TimeZone.getAvailableIDs that are in America. Strip * off the * "America/" prefix and sort the result. * * @return */ privatedefamericaTimezones: Array[String] = { TimeZone.getAvailableIDs.filter(_.startsWith("America/")).map(_.stripPrefix("America/")).sorted }
privatedefamericaTimezones2: Array[String] = { // no need to sort TimeZone.getAvailableIDs.filter(_.startsWith("America/")).map(_.drop("America/".length)) }
ex 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * Ex 11: * Import java.awt.datatransfer._ and make an object of type SystemFlavorMap with the call * val flavors = SystemFlavorMap.getDefaultFlavorMap().asInstanceOf[SystemFlavorMap] * Then call the getNativesForFlavor method with parameter DataFlavor.imageFlavor and get the return value as a * Scala buffer. (Why this obscure class? It’s hard to find uses of java.util.List in the standard Java library.) * * @return */ privatedefgetFlavors: mutable.Buffer[String] = { import scala.collection.JavaConversions.asScalaBuffer val flavors = SystemFlavorMap.getDefaultFlavorMap.asInstanceOf[SystemFlavorMap] flavors.getNativesForFlavor(DataFlavor.imageFlavor) }