javaHashMapLinkedHashMapTreeMapWord下载.docx
《javaHashMapLinkedHashMapTreeMapWord下载.docx》由会员分享,可在线阅读,更多相关《javaHashMapLinkedHashMapTreeMapWord下载.docx(12页珍藏版)》请在冰豆网上搜索。
![javaHashMapLinkedHashMapTreeMapWord下载.docx](https://file1.bdocx.com/fileroot1/2022-11/16/741c748b-3d90-4754-b5a7-b91e1d9f6fbf/741c748b-3d90-4754-b5a7-b91e1d9f6fbf1.gif)
2014
年10月22
日
软件环境:
WindowsXP实验工具:
MyEclipse
硬件环境:
CPU3.0GHZ,内存2G
一、实验目的
1、掌握文件类File的基本用法。
2、掌握Map、HashMap、LinkedHashMap、TreeMap等用法。
3、掌握迭代器的使用。
二、实验过程及内容
1、创建java项目,创建各个类。
2、根据要求编写各种类的程序。
3、代码如下:
✧
packagecn.edu.hncj;
importjava.io.File;
classTheFile{
publicstaticvoidmain(String[]args){
FilediskC=newFile("
C:
/"
);
FiletestFile=newFile(diskC,"
test"
System.out.println(diskC.getAbsolutePath()+"
是否存在:
"
+diskC.exists());
System.out.println(testFile.getAbsolutePath()+"
+testFile.exists());
}
}
importjava.io.IOException;
classTheFile2{
publicstaticvoidmain(String[]args){
FiletestFile=newFile("
c:
\\test"
System.out.println(testFile.getAbsolutePath()+"
try{
testFile.createNewFile();
catch(IOExceptione){
e.printStackTrace();
FiletestDir=newFile("
\\a\\b\\c"
System.out.println(testDir.getAbsolutePath()+"
+testDir.exists());
testDir.mkdirs();
importjava.util.*;
publicclassHashMapDemo{
publicstaticvoidmain(String[]args){
HashMap<
Integer,String>
map=newHashMap<
();
map.put(1,"
pds"
map.put(2,"
sq"
map.put(3,"
ly"
map.put(4,"
fs"
//map.put(4,"
xy"
System.out.println("
containsKey:
+map.containsKey
(1));
System.out.println(map);
get:
+map.get
(2));
map.put(5,null);
+map.get(null));
Collection<
String>
coll=map.values();
System.out.println(coll);
publicclassHashMapDemo2{
//LinkedHashMap<
map=newLinkedHashMap<
//TreeMap<
map=newTreeMap<
Iterator<
Integer>
it=map.keySet().iterator();
while(it.hasNext()){
Integeri=(Integer)it.next();
System.out.println(i+"
:
+map.get(i));
}
packagecn.edu.hncj;
classHashMapDemo1{
publicstaticvoidmain(Stringargs[]){
HashMaphm=newHashMap();
hm.put("
John"
newDouble(34.34));
Tom"
newDouble(23.22));
Jane"
newDouble(78.00));
Smith"
newDouble(99.22));
Rose"
newDouble(-19.88));
Iterator<
it=hm.keySet().iterator();
while(it.hasNext()){
Stringi=(String)it.next();
System.out.println("
key:
+i+"
value:
+hm.get(i));
三、实验结果
实验结果如下:
图1
图2
图3
图4
图5
图6
图7
图8
图9
图10
图11
图12
图13
四、出现问题及解决方案
在这次试验中SortedMap程序不会写,其他的Map类只会写些简单的。
这次的实验前两个是照这课本打的,后几个是自己编的,其中的功能是因为程序太短XX的一些功能,后面的Map类比较了HashMap、LinkedHashMap、TreeMap的区别。