《快学Scala》, <Scala For the Impatient>习题第三章

Scala数组操作技巧
本文介绍Scala中数组操作的实用技巧,包括生成随机整数数组、交换数组元素、按条件重新排序数组元素及计算Double数组平均值的方法。
1. Write a code snippet that sets a to an array of n random integers between 0 (inclusive) and n (exclusive).
  def genRandomArray(n: Int) : Array[Int] = for (i <- 0.until(n).toArray) yield scala.util.Random.nextInt(n - 1)
2. Write a loop that swaps adjacent elements of an array of integers. For example, Array(1, 2, 3, 4, 5) becomes Array(2, 1, 4, 3, 5).

scala> :paste
// Entering paste mode (ctrl-D to finish)

 def swapArrayInPlace(arr: Array[Int]): Unit = {
    var temp = 0;
    for (i <- 0.until(arr.length, 2)) {
      if (i < arr.length - 2) {
        temp = arr(i + 1)
        arr(i + 1) = arr(i)
        arr(i) = temp
      }
    }
  }


// Exiting paste mode, now interpreting.

swapArrayInPlace: (arr: Array[Int])Unit
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)

scala> swapArrayInPlace(a)

scala> a
res71: Array[Int] = Array(2, 1, 4, 3, 5)
3. Repeat the preceding assignment, but produce a new array with swapped values. Use for/yield.
scala> :paste
// Entering paste mode (ctrl-D to finish)

  def swapArray(arr: Array[Int]) : Array[Int] = for (i <- arr.indices.toArray) yield {
    if (i == arr.length - 1)
      arr(i)
    else if (i % 2 == 0)
      arr(i+1)
    else arr(i-1)
  }

// Exiting paste mode, now interpreting.

swapArray: (arr: Array[Int])Array[Int]

scala> swapArray(Array(1,2,3,4,5))
res60: Array[Int] = Array(2, 1, 4, 3, 5)
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.
  def reorder(arr: Array[Int]) : Array[Int] = {
    arr.filter(_ > 0) ++ arr.filter(_ <= 0)
  }

scala> reorder(Array(4,-1,0, 52,15,6,-7))
res72: Array[Int] = Array(4, 52, 15, 6, -1, 0, -7)
5. How do you compute the average of an Array[Double]?
<?xml version="1.0" encoding="UTF-8"?> 4.0.0 <groupId>org.example</groupId> <artifactId>scala_tutorial</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <scala.version>2.12.11</scala.version> <spark.version>3.1.1</spark.version> <hadoop.version>3.2.4</hadoop.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <!-- Scala Library --> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <!-- Spark Core --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.12</artifactId> <version>${spark.version}</version> </dependency> <!-- Spark SQL --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.12</artifactId> <version>${spark.version}</version> </dependency> <!-- Spark Hive Support --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-hive_2.12</artifactId> <version>${spark.version}</version> </dependency> <!-- Hadoop Client --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> </dependency> </dependencies> <build> <plugins> <!-- Scala Compiler Plugin --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>4.8.1</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <scalaVersion>${scala.version}</scalaVersion> </configuration> </plugin> <!-- Maven Compiler Plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> <!-- Maven Assembly Plugin for creating fat JAR --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.6.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> 版本不变,给我调整个能用的
最新发布
11-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值