Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
caopu16
whatsmars
提交
c39556d2
W
whatsmars
项目概览
caopu16
/
whatsmars
与 Fork 源项目一致
Fork自
武汉红喜 / whatsmars
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
whatsmars
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c39556d2
编写于
6月 16, 2016
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
unmodifiableMap
上级
7166033c
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
71 addition
and
0 deletion
+71
-0
whatsmars-javase-example/src/main/java/com/itlong/whatsmars/base/collection/Point.java
...main/java/com/itlong/whatsmars/base/collection/Point.java
+26
-0
whatsmars-javase-example/src/main/java/com/itlong/whatsmars/base/collection/UnmodifiableTest.java
...om/itlong/whatsmars/base/collection/UnmodifiableTest.java
+45
-0
未找到文件。
whatsmars-javase-example/src/main/java/com/itlong/whatsmars/base/collection/Point.java
0 → 100644
浏览文件 @
c39556d2
package
com.itlong.whatsmars.base.collection
;
/**
* Created by shenhongxi on 2016/6/14.
*/
public
class
Point
{
private
float
x
;
private
float
y
;
public
Point
(
float
x
,
float
y
){
setLocation
(
x
,
y
);
}
public
void
setLocation
(
float
x
,
float
y
){
this
.
x
=
x
;
this
.
y
=
y
;
}
public
float
getX
(){
return
x
;
}
public
float
getY
(){
return
y
;
}
}
whatsmars-javase-example/src/main/java/com/itlong/whatsmars/base/collection/UnmodifiableTest.java
0 → 100644
浏览文件 @
c39556d2
package
com.itlong.whatsmars.base.collection
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* Created by shenhongxi on 2016/6/14.
*/
public
class
UnmodifiableTest
{
private
Map
<
String
,
Point
>
startingLocations
=
new
HashMap
<
String
,
Point
>(
3
);
public
UnmodifiableTest
(){
startingLocations
.
put
(
"LeftRook"
,
new
Point
(
1
,
1
));
startingLocations
.
put
(
"LeftKnight"
,
new
Point
(
1
,
2
));
startingLocations
.
put
(
"LeftCamel"
,
new
Point
(
1
,
3
));
//..more locations..
}
public
Map
<
String
,
Point
>
getStartingLocations
(){
return
Collections
.
unmodifiableMap
(
startingLocations
);
}
public
static
void
main
(
String
[]
args
){
UnmodifiableTest
pieceLocations
=
new
UnmodifiableTest
();
Map
<
String
,
Point
>
locations
=
pieceLocations
.
getStartingLocations
();
Point
camelLoc
=
locations
.
get
(
"LeftCamel"
);
System
.
out
.
println
(
"The LeftCamel's start is at [ "
+
camelLoc
.
getX
()
+
", "
+
camelLoc
.
getY
()
+
" ]"
);
//Try 1. update elicits Exception
try
{
locations
.
put
(
"LeftCamel"
,
new
Point
(
0
,
0
));
}
catch
(
java
.
lang
.
UnsupportedOperationException
e
){
System
.
out
.
println
(
"Try 1 - Could not update the map!"
);
}
//Try 2. Now let's try changing the contents of the object from the unmodifiable map!
camelLoc
.
setLocation
(
0
,
0
);
//Now see whether we were able to update the actual map
Point
newCamelLoc
=
pieceLocations
.
getStartingLocations
().
get
(
"LeftCamel"
);
System
.
out
.
println
(
"Try 2 - Map updated! The LeftCamel's start is now at [ "
+
newCamelLoc
.
getX
()
+
", "
+
newCamelLoc
.
getY
()
+
" ]"
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录