Scala 挑战练习题 day-02

本文介绍了Scala中的WordCount函数,用于统计字符串中单词数量,随后展示了如何编写minAndMax函数获取数组的最小值和最大值,以及移除链表中的零元素。还涉及了映射数组操作、二维数组转换和处理字符串集合的函数。
摘要由CSDN通过智能技术生成
数据结构篇:

    package com.qf.scala.exercise
    
    import scala.collection.mutable
    
    //编写一段WordCount函数,
    // 统计传入的字符串中单词的个数
    object _dya02Ex01 {
      def main(args: Array[String]): Unit = {
          //调用
          wordCount("anything is possible believe yourself")
    
          //定义wordCount函数
          def wordCount(str:String)={
              val count = new mutable.HashMap[String,Int]
              for(words <- str.split(" ")) {
                  count(words) = count.getOrElse(words,0) + 1
              }
          }
      }
    }
    

    package com.qf.scala.exercise
    //编写一个函数 minmax(values:Array[Int]),
    // 返回数组中最小值和最大值的对偶
    object _dya02Ex02 {
        def main(args: Array[String]): Unit = {
    
            def minAndMax(v:Array[Int]) ={
                (v.min,v.max)
            }
            val ints = Array(1,2,3,4,5,6)
            println(minAndMax(ints))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值