实验五集合类与泛型
1.实验目的
(1)掌握ArrayList类与LinkedList类的用法;
(2)掌握TreeSet类的用法;
(3)掌握Map接口及其实现类的用法
(4)掌握泛型的用法。
2.实验内容
实验题1 有四个类,主类Store在包https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类, Product类实现Comparable接口,重写了Comparable接口中方法compareTo,实现了product对象按照价格排序。
基本要求:
(1)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用ArrayList与LinkedList存放,最后用StringBuiler存放并将其输出。
(2)用迭代器(iterator)将实例对象输出(要求用加强型for循环)。
Store类
package https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p44;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Mobile;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Mp3Player;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Product;
public class Store {
public static int count=0;
public static void main(String[] args) {
Mobile m1=new Mobile("E365 on china Mobile",1780);
Mobile m2=new Mobile("E3330 on china Mobile",1450);
Mp3Player mp1=new Mp3Player("Meiz0 X3 (256MB)",399);
Mp3Player mp2=new Mp3Player("Meiz0 E5 (512MB)",580);
Mp3Player mp3=new Mp3Player("Xlive XM Mp3Player(256MB)",930);
Product p[]={mp2,mp1,mp3,m1,m2};
Arrays.sort(p);
StringBuilder text=new StringBuilder();
for(Product i:p){
text.append(i).append("\n");
}
System.out.println("用StringBuiler输出:\n"+text);
List
List
for(int i=0;i
list1.add(p[i]);
for(int i=0;i
list2.add(p[i]);
Iterator
System.out.println(" 用 ArrayList或LinkedList迭代器输出:");
while(it.hasNext()){
Product p1=it.next();
System.out.println(p1);
}
}
}
实验题2 项目包含类见实验题1。
基本要求:
(1)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用TreeSet存放,最后用StringBuiler存放并将其输出。
(2)用迭代器(iterator)将实例对象输出。
Store类
package https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p44;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JOptionPane;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Mobile;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Mp3Player;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Product;
public class Store {
public static int count=0;
public static void main(String[] args) {
Mobile m1=new Mobile("E365 on china Mobile",1780);
Mobile m2=new Mobile("E3330 on china Mobile",1450);
Mp3Player mp1=new Mp3Player("Meiz0 X3 (256MB)",399);
Mp3Player mp2=new Mp3Player("Meiz0 E5 (512MB)",580);
Mp3Player mp3=new Mp3Player("Xlive XM Mp3Player(256MB)",930);
Product p[]={mp2,mp1,mp3,m1,m2};
Arrays.sort(p);
StringBuilder text=new StringBuilder();
for(Product i:p){
text.append(i).append("\n");
}
System.out.println("用StringBuiler输出:\n"+text);
Set
for(int i=0;i
set1.add(p[i]);
Iterator
System.out.println(" 用TreeSet迭代器输出:");
while(it.hasNext()){
Product p1=it.next();
System.out.println(p1);
}
}
}
实验题3 项目包含类见实验题1。
基本要求:
(1)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用HashMap与TreeMap 存放,最后用StringBuiler存放并将其输出。
(2)用迭代器(iterator)将实例对象输出。
package https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p44;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JOptionPane;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Mobile;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Mp3Player;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Product;
public class Store {
public static int count=0;
public static void main(String[] args) {
Mobile m1=new Mobile("E365 on china Mobile",1780);
Mobile m2=new Mobile("E3330 on china Mobile",1450);
Mp3Player mp1=new Mp3Player("Meiz0 X3 (256MB)",399);
Mp3Player mp2=new Mp3Player("Meiz0 E5 (512MB)",580);
Mp3Player mp3=new Mp3Player("Xlive XM Mp3Player(256MB)",930);
Product p[]={mp2,mp1,mp3,m1,m2};
Arrays.sort(p);
StringBuilder text=new StringBuilder();
for(Product i:p){
text.append(i).append("\n");
}
System.out.println("用StringBuiler输出:\n"+text);
Map
for(int i=0;i
map1.put(p[i].getPrice(), p[i].getName());
}
Set
set1=map1.entrySet();
Iterator
it=set1.iterator();
System.out.println("用迭代器输出:");
while(it.hasNext()){
Map.Entry
System.out.println(a.getValue()+" "+a.getKey());
}
}
}
因为HashMap与TreeMap的存放与用迭代器方法一样,这里只列出HashMap 的
实验题4 有四个类,主类Store在包https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类。
基本要求:
(1)设计比较器类ProductComparator类,ProductComparator类实现接口Comparator接口,重写其中compare(Object object1,Object object2)方法,实现了product对象按照名称排序。
(2)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用HashMap与TreeMap 存放,最后用StringBuiler存放并将其输出。
(3)用迭代器(iterator)将实例对象输出。
Store类
package https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p44;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JOptionPane;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Mobile;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Mp3Player;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.Product;
import https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data.ProductComparator;
public class Store {
public static int count=0;
public static void main(String[] args) {
Mobile m1=new Mobile("E365 on china Mobile",1780);
Mobile m2=new Mobile("E3330 on china Mobile",1450);
Mp3Player mp1=new Mp3Player("Meiz0 X3 (256MB)",399);
Mp3Player mp2=new Mp3Player("Meiz0 E5 (512MB)",580);
Mp3Player mp3=new Mp3Player("Xlive XM Mp3Player(256MB)",930);
Product p[]={mp2,mp1,mp3,m1,m2};
Arrays.sort(p,new ProductComparator());
StringBuilder text=new StringBuilder();
for(Product i:p){
text.append(i).append("\n");
}
System.out.println("用StringBuiler输出:\n"+text);
// Map
/*for(int i=0;i
map1.put(p[i].getPrice(), p[i].getName());
}*/
Map
for(int i=0;i
map1.put(p[i].getName(),p[i]);
Collection
Iterator
System.out.println("用迭代器输出:");
while(it.hasNext()){
System.out.println(it.next());
}
/* Set
set1=map1.entrySet();
Iterator
it=set1.iterator();
System.out.println("用迭代器输出:");
while(it.hasNext()){
Map.Entry
System.out.println(a.getValue()+" "+a.getKey());
}*/
}
}
ProductComparator类
package https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data;
import https://www.sodocs.net/doc/7a15018400.html,parator;
public class ProductComparator implements Comparator {
@Override
public int compare(Object arg0, Object arg1) {
if(((Product)arg0).getName().compareTo(((Product)arg1).getName())<0) return -1;
if(((Product)arg0).getName().compareTo(((Product)arg1).getName())>0) return 1;
return 0;
}
}
因为HashMap与TreeMap的存放与用迭代器方法一样,这里只列出HashMap 的
实验总结
这次实验比较简单,但是我用HashMap输出的内容排序却和StringBuilder不一样,这个是个问题,需要重写HASHCODE()与EQUALS()方法,其余的只要对着书写就没什么问题。
实验十泛型与集合框架 1.实验目的 1、掌握LinkedList
《集合框架及泛型》作业 一、根据课上讲解内容,完成演示示例和课堂练习 1、ArrayList获取并打印新闻标题 需求说明:按照以下实现的步骤,使用ArrayList获取和打印新闻标题,显示效果如下图所示: (1)创建多个各类新闻标题对象,包含ID、名称和创建者三个属性; (2)创建存储各类新闻标题的集合对象; (3)按照顺序依次添加各类新闻标题,使用add()方法; (4)获取新闻标题的总数,使用size()方法; (5)根据位置获取相应新闻标题、逐条打印每条新闻标题的名称,使用for 循环遍历。 2、ArrayList存储狗狗信息 需求说明:按照以下实现的步骤,使用ArrayList存储狗狗信息,使用ArrayList的方法对狗狗信息进行删除、读取和判断,显示效果如下图所示:(1)存储多条狗信息,获取狗总数,逐条打印出各条狗信息; (2)删除指定位置的狗,使用remove()方法; (3)判断集合中是否包含指定狗,使用contains()方法;
3、LinkedList添加和删除新闻标题 需求说明:在作业1的基础上,换用LinkedList存储新闻数据,并且使用LinkedList的getFirst()和getLast()方法获取第一条和最后一条数据,以及removeFirst()和removeLast()方法删除第一条和最后一条数据,输出效果如下图所示。 4、集合头尾位置删除和条件狗信息 需求说明:按照作业3的实现方式和所用到LinkedList的方法,实现狗狗信
息的更新并输出,输出效果如图所示。 5、使用Iterator和增强型for循环遍历Set 需求说明:按照以下实现的步骤,使用Iterator和增强型for循环遍历Set,输出效果如下图所示: (1)创建多个各类新闻标题对象,包含ID、名称和创建者三个属性; (2)创建存储各类新闻标题的集合对象; (3)按照顺序依次添加各类新闻标题; (4)获取新闻标题的总数; (5)使用iterator()获取Iterator对象; (6)使用Iterator遍历集合,使用hasNext()方法作为循环条件,判断是否存在另一个可访问的元素; (7)使用增强型for遍历集合;
实验内容: (1)定义一个泛型类Instrument
实验内容: (1)定义一个泛型类In strume nt
Java 语言程序设计 C 实验报告 集合框架及泛型机制 学生姓名 专业、班级 指导教师 成绩 计算机与信息工程学院 年月日
一、实验目的 学习课程相关章节知识,通过上机练习,掌握以下知识: 1.掌握 List 接口下 ArrayList 及 LinkedList 的使用方法。 2.掌握 Map 接口下 HashMap 及 HashTable的使用方法 3.掌握集合中泛型的使用 二、实验内容 利用集合完成象数据库那样存储数据,并且可以简单查询,利用 map 存储学生信息,字段如下: id ,name,age,实现步骤: (1)创建类,类图如下: (2)在 main 方法编写逻辑代码 (3)运行程序并测试结果 package https://www.sodocs.net/doc/7a15018400.html,; public class Student { private String name ; private int age ; private String id ;
public String getName() { return name ; } public void setName(String name ) { this . name =name ; } public int getAge() { return age ; } public void setAge(int age ) { this. age=age ; } public String getId() { return id; } public void setId(String id) { this. id=id; } public Student(String name ,int age , String id ) { super(); this. name =name ; this. age=age ; this. id=id; } public void sayHi() { System.out.println("name=" +this.getName()+"age=" + this .getAge()+" " + "id=" + this.getId()); } }
实验内容: (1) 定义一个泛型类Instrument<E 〉,其中包括一个泛型方法void pla y(E x)。 定义两种乐器类:Cello、Violin 可以进行演奏。定义一个测试类进行测试. package ex111; public class Violin{ publicString toString() { System、out、println("violin、、、、、、、、、"); return ””; } } package ex111; public class Test_Main{ public staticvoid main(String[]args){ instrument〈Cello>model = new instrument〈Cello〉(); instrument 6泛型与集合 6.1单项选择题 1.可实现有序对象的操作是?() A.HashMap B.HashSet C.TreeMap D.Stack 2.不是迭代器接口(Iterator)所定义的方法是()。 A.hasNext() B.next() C.remove() D.nextElement() 3.下面说法不正确的是() A.列表(List)、集合(Set)和映射(Map)都是java.util包中的接口。 B.List接口是可以包含重复元素的有序集合。 C.Set接口是不包含重复元素的集合。 D.Map接口将键映射到值,键可以重复,但每个键最多只能映射一个值。 4.下面那些方法不是接口Collection中已声明的方法() A.添加元素的add(Object obj) 方法 B.删除元素的remove(Object obj)方法 C.得到元素个数的length()方法 D.返回迭代器的iterator()方法,迭代器用于元素遍历 5. 下列关于容器的描述中,错误的是() A.容器是由若干个组建和容器组成的 B.容器是对图形界面中界面元素的一种管理 C.容器是一种对指定宽和高的矩形范围 D.容器都是可以独立的窗口 6. 下列界面元素中,不是容器的是() A.List B.JFrame C.JDialog D.Panel 7.应用程序的main方法中有以下语句,则输出的结果是( )。 Hashtable hashtable=new Hashtable(); hashtable.put("100","aaa"); hashtable.put("200","bbb"); hashtable.put("300","ccc"); System.out.println(hashtable.get("300").toString() + hashtable.get("200").toString() + hashtable.get("100").toString()); A) aaa B) bbb C) ccc D) cccbbbaaa 6.2判断题 1.Map接口是自Collection接口继承而来。(×) 2.集合Set是通过键-值对的方式来存储对象的。(×) 3.Integer i = (Integer.valueOf("926")).intValue();(√) 4.String s = (Double.valueOf("3.1415926")).toString(); (√) 5.Integer I = Integer.parseInt("926");(√) 6.Arrays类主要对数组进行操作。(√) 7.在集合中元素类型必须是相同的。(√) 8.集合中可以包含相同的对象。(×) 9.枚举接口定义了具有删除功能的方法。(×) 6.3程序阅读题 1.阅读下面的程序,回答问题。 import java.util.*; public class T { public static void main(String args[]) { Set set = new TreeSet(); set.add(new Integer(10)); set.add(new Integer(5)); set.add(new Integer(15)); set.add(new Integer(5)); set.add(new Integer(10)); System.out.println("size = " + set.size()); Iterator it=set.iterator(); while(it.hasNext()){ System.out.print(it.next()+" "); } } 实验七Java集合与泛型 一、实验目的 1)掌握集合的概念、体系结构、分类及使用场景 2)了解Set接口及主要实现类(HashSet、TreeSet) 3)了解List接口及主要实现类(ArrayList、LinkedList、Vector) 4)掌握ArrayList的使用 5)掌握ArrayList与Vector的区别 6)了解Map接口及主要实现类(HashMap、TreeMap、HashTable) 7)掌握HashMap的使用 8)掌握HashMap与HashTable的区别 二、实验环境 JDK1.6+Eclpise3.2 三、实验准备 1)复习课件中理论知识 2)练习课堂所讲的例子 四、实验内容 1、编写程序练习List集合的基本使用: 1) 创建一个只能容纳String对象名为names的ArrayList集合; 2)按顺序往集合中添加5个字符串对象:“张三”、“李四”、“王五”、“马六”、“赵七”; 3)对集合进行遍历,分别打印集合中的每个元素的位置与内容; 4)首先打印集合的大小,然后删除集合中的第3个元素,并显示删除元素的内容,然后再打印目前集合中第3个元素的内容,并再次打印集合的大小。 2、编写程序练习Map集合的基本使用: 1)创建一个只能值只能容纳String对象的person的HashMap集合; 2)往集合中添加5个“键-值”对象:id—>”1”、name—>”张三”、sex—>”男”、age—>”25”、love—>”爱学Java” 3)对集合进行遍历,分别打印集合中的每个元素的键与值; 4)首先打印集合的大小,然后删除集合中的键为age的元素,并显示删除元素的内容,并再次打印集合的大小。 五、验过程及结果 第1题调试结果如下图: 选择题 1.在java中,( C )对象可以使用键/值的形式保存数据。 A:ArrayList B: HashSet C: HashMap D: LinkedList 2.创建一个类,来存储唯一的对象元素,元素不需要保持特定顺序,但是必须唯一。最 能满足这种要求的接口是:( AE ) 。(多选) A、Set B、List C、Map D、Array E、HashSet 3.以下不属于ArrayList的方法的是(C ) A: add() B: addAll() C: addFirst() D: size(); 4.下列声明语句错误的是(C ) A : List list=new ArrayList() B: List Java语言程序设计C 实验报告 集合框架及泛型机制 学生姓名 专业、班级 指导教师 成绩 计算机与信息工程学院 年月日 一、实验目的 学习课程相关章节知识,通过上机练习,掌握以下知识: 1.掌握List接口下ArrayList及LinkedList的使用方法。 2.掌握Map接口下HashMap 及HashTable的使用方法 3.掌握集合中泛型的使用 二、实验内容 利用集合完成象数据库那样存储数据,并且可以简单查询,利用map存储学生信息,字段如下: id ,name,age,实现步骤: (1)创建类,类图如下: (2)在main方法编写逻辑代码 (3)运行程序并测试结果 package com、cn; public class Student { private String name; private int age; private String id; public String getName() { return name; } public void setName(String name) { this、name = name; } public int getAge() { return age; } public void setAge(int age) { this、age = age; } public String getId() { return id; } public void setId(String id) { this、id = id; } public Student(String name, int age, String id) { super(); this、name = name; this、age = age; this、id = id; } public void sayHi() { System、out、println("name="+this、getName()+"age="+this、getAge()+" "+"id="+this、getId()); } } //Databace类 package com、cn; import java、util、Collection; import java、util、HashMap; import java、util、Iterator; public class Databace { private Student a; public Databace() { super(); map=new HashMap Java集合与泛型 一、实验目的 1)掌握集合的概念、体系结构、分类及使用场景2)了解Set接口及主要实现类(HashSet、TreeSet) 3)了解List接口及主要实现类(ArrayList、LinkedList、Vector)4)掌握ArrayList的使用5)掌握ArrayList与Vector的区别 6)了解Map接口及主要实现类(HashMap、TreeMap、HashTable)7)掌握HashMap的使用 8)掌握HashMap与HashTable的区别 二、实验环境 JDK1.6+Eclpise3.2 三、实验准备 1)复习课件中理论知识2)练习课堂所讲的例子 四、实验内容 1、编写程序练习List集合的基本使用: 1) 创建一个只能容纳String对象名为names的ArrayList集合; 2)按顺序往集合中添加5个字符串对象:“张三”、“李四”、“王五”、“马六”、“赵七”;3)对集合进行遍历,分别打印集合中的每个元素的位置与内容; 4)首先打印集合的大小,然后删除集合中的第3个元素,并显示删除元素的内容,然后再打印目前集合中第3个元素的内容,并再次打印集合的大小。 2、编写程序练习Map集合的基本使用: 1)创建一个只能值只能容纳String对象的person的HashMap集合; 2)往集合中添加5个“键-值”对象:id—>”1”、name—>”张三”、sex—>”男”、age —>”25”、love—>”爱学Java” 3)对集合进行遍历,分别打印集合中的每个元素的键与值; 4)首先打印集合的大小,然后删除集合中的键为age的元素,并显示删除元素的内容,并再次打印集合的大小。 任务一:用LinkedList存放对象 1.利用面向对象的思想,创建以下类: ●Person类,包含Person的姓名和身份证号码,覆盖Object类的toString() 方法,显示“姓名:XXX 身份证号:XXX”。 ●Student类,继承Person类,包含学生的语文、数学、英文课的成绩,并覆盖 父类的toString()方法,显示“姓名:XXX 身份证号:XXX 语文:XXX 数学:XXX 英文:XXX”。 ●Teacher类,继承Person类,包含教师的工资。并覆盖父类的toString()方 法,显示“姓名:XXX 身份证号:XXX 工资:XXX”。 ●public class Person implements Comparable{ ●String name; ●String ID; ●Person(String s,String i){ ●name=s; ●ID=i; } ●public String toString() { ●String str="姓名:"+name+" 身份证号码:"+ID; ●return str; } ●public int compareTo(Object arg0) { ●Person p=(Person)arg0; ●return https://www.sodocs.net/doc/7a15018400.html,pareTo(p.ID); } } ●class Student extends Person { ●int Chinese; ●int Math; ●int English; ●Student(String n,String i,int c,int m,int e){ ●super(n,i); ●Chinese=c; ●Math=m; ●English=e; } ●public String toString() { ●String str; ●str=" 语文成绩:"+Chinese+" 数学成绩:"+Math+" 英语成绩: "+English; ●return super.toString()+str; ●} ●} ●class Teacher extends Person{ ●int salary; ●Teacher(String n,String i,int s){ ●super(n,i); ●salary=s; ●} 泛型与集合框架 1.实验目的 1、掌握LinkedList 实验五集合类与泛型 1.实验目的 (1)掌握ArrayList类与LinkedList类的用法; (2)掌握TreeSet类的用法; (3)掌握Map接口及其实现类的用法 (4)掌握泛型的用法。 2.实验内容 实验题1 有四个类,主类Store在包https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类, Product类实现Comparable接口,重写了Comparable接口中方法compareTo,实现了product对象按照价格排序。 基本要求: (1)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用ArrayList与LinkedList存放,最后用StringBuiler存放并将其输出。 (2)用迭代器(iterator)将实例对象输出(要求用加强型for循环)。 实验题2 项目包含类见实验题1。 基本要求: (1)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用TreeSet存放,最后用StringBuiler存放并将其输出。 (2)用迭代器(iterator)将实例对象输出。 实验题3 项目包含类见实验题1。 基本要求: (1)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用HashMap与TreeMap 存放,最后用StringBuiler存放并将其输出。 (2)用迭代器(iterator)将实例对象输出。 *实验题4 有四个类,主类Store在包https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包https://www.sodocs.net/doc/7a15018400.html,.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类。 基本要求: (1)设计比较器类ProductComparator类,ProductComparator类实现接口Comparator接口,重写其中compare(Object object1,Object object2)方法,实现了product对象按照名称排序。 (2)在主类Store中实例化多个Mobile类与Mp3Player的实例,分别将这些实例用HashMap与TreeMap 存放,最后用StringBuiler存放并将其输出。 (3)用迭代器(iterator)将实例对象输出。 浙江大学城市学院实验报告 课程名称面向对象程序设计 实验项目名称集合框架与泛型 学生姓名专业班级学号 一. 实验目的和要求 1. 了解Java集合框架的接口和实现类 2. 理解泛型类、泛型接口、泛型方法的特点 3. 掌握List 实验报告_实验7 泛型与集合(学生学号_姓 名) 一、实验目的 1.理解泛型的概念及泛型的简单使用 2.掌握集合接口及其实现类的概念; Set接口:HashSet、TreeSet、 List接口:ArrayList、LinkedList、Vector Map接口:HashMap、TreeMap、Hashtable 3.掌握集合元素的遍历方法; 4.熟悉Java中的装箱、拆箱操作。 二、实验步骤 3、(基础题)填充VectorTest.java程序所缺代码,编译并运行程序; 程序所缺代码: Vector vec = new _____①___Vector__(); vec. ②add(number); for(int i = 0; i < vec.____③size()_____; i++){ System.out.println(vec.____④_get___(i)); 4、(基础题)阅读并运行下面代码,比较Set接口、List接口、Map接口的异同点,并回答问题,: 问题:如何为程序中的集合指定类型? Set for(int i = 0; i < 100000; i++) { table. put②(""+i, new Integer(i)); } for(int i = 0; i < 100000; i++){ table. ___get_③_____ (""+i); } for(int i = 0; i < 100000; i++){ list. ___add_④____ (new Integer(i)); } callTime = ____System.currentTimeMillis();___⑤_______ for(int i = 0; i < 100000; i++){ list.____get_⑥______(i); } 问题: 在读取数据时, Hashtable与LinkedList相比,哪个效率更高? Hashtable 6、(基础题)本程序的功能是:获取系统的系统属性值并输出,请参考课件,填充所缺少代码,并运行程序: 程序所缺代码: Properties systemProperties = System. _____①getProperties ______ (); Enumeration enum1 = systemProperties. ②propertyNames(); while(enum1. ____③__ hasMoreElements ___ ()){ String key = (String)enum1.nextElement(); String pro = systemProperties. ____④_ getProperty ___ (key); System.out.println(key + "=" + pro); } } } Collection 接口及实现类 Java 语言的Collection 接口及实现类是在java.util 包中定义的,其中定义了多个接口和类,它们统称为Java 集合框架(Java Collection Framework )。 Java 集合框架由两种类型构成,一个是Collection ,另一个是Map 。Collection 对象用于存放一组对象,Map 对象用于存放一组关键字/值的对象。Collection 和Map 是最基本的接口,它们又有子接口,这些接口的层次关系如图1所示。 图1 Java 集合框架的接口继承关系 1.1 Collection 接口及操作 Collection 接口是所有集合类型的根接口,它有三个子接口:Set 接口、List 接口和Queue 接口。 Collection 接口的定义如下: public interface Collection 实验13 集合框架与泛型 一、实验目的和要求 1. 了解Java集合框架的接口和实现类 2. 理解泛型类、泛型接口、泛型方法的特点 3. 掌握List 实验二 LINQ、集合与泛型 一、实验目的 1.掌握LINQ的用法; 2.掌握C#典型集合类的用法; 3.理解C#泛型。 二、实验内容 (实验过程中编写的程序复制到本文件中,下课整理后上交。如对C#或.NET Framework相关内容不熟悉,可参考MSDN文档。) 1.Querying an Array of Invoice(发票,发货单) Objects Use the class Invoice to create an array of Invoice https://www.sodocs.net/doc/7a15018400.html,e the sample data shown in Fig. 1.Class Invoice includes four properties—a PartNumber (type int), a PartDescription (type string), a Quantity of the item being purchased (type int) and a Price (type decimal). Perform the following queries on the array of Invoice objects and display the results: a) Use LINQ to sort the Invoice objects by PartDescription. b) Use LINQ to sort the Invoice objects by Price. c) Use LINQ to select the PartDescription and Quantity and sort the results by Quantity. d) Use LINQ to select from each Invoice the PartDescription and the value of the Invoice (i.e., Quantity * Price). Name the calculated column InvoiceTotal. Order the results by Invoice value. [Hint: Use let to store the result of Quantity * Price in a new range variable total.] e) Using the results of the LINQ query in Part d, select the InvoiceTotals in the range $200 to $500.6Java第六单元练习题-泛型与集合
实验七:Java集合与泛型
集合及泛型自测题
JAVA实验报告-集合框架及泛型机制
集合实验
实验6 泛型与集合框架_附答案
泛型与集合框架
实验04_集合类与泛型
学号姓名--集合框架与泛型实验报告
实验报告_实验7 泛型与集合(学生学号_姓名)
java 集合与泛型
实验13集合框架与泛型实验报告
C#实验LINQ、集合与泛型