数据结构接口编程练习题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验六数据结构接口编程
实验目的
掌握java.util.Collection接口的特点和使用方法、掌握java.util.Set接口的实现类的特点和使用方法、java.util.List接口的实现类的特点和使用方法、java.util.Map接口的实现类的特点和使用方法、Iterator接口的使用方法、对List进行排序的方法。
实验要求
编写一个包括Set、List、Map,以及运用这些类的方法的应用程序。
实验内容
1.创建java.util.Collection接口的实现类;
2.创建java.util.Set接口的实现类;
3.创建java.util.List接口的实现类;
4.创建java.util.Map接口的实现类;
5.Iterator接口的使用方法;
6.对List进行排序的使用方法。
1、Collection接口的使用方法
import java.util.*;
public class TestCollection {
public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Name("f1","l1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c);
}
}
class Name {
private String firstName,lastName;
public Name(String firstName, String lastName) {
this.firstName = firstName; stName = lastName;
}
public String getFirstName() { return firstName; }
public String getLastName() { return lastName; }
public String toString() { return firstName + " " + lastName; } }
2、创建Set实现类对象,并使用Set接口实现类的方法
import java.util.*;
class Name1 {
String firstName;
String lastName;
Name1(String firstName ,String lastName) {
this.firstName = firstName;
stName = lastName;
}
}
public class TestSet {
public static void main(String[] args) {
Collection s = new HashSet();
s.add("hello");
s.add("world");
s.add(new Name1("f1","f2"));
s.add(new Integer(100));
s.add("hello");
System.out.println(s);
}
}
3、创建List实现类对象,并使用List接口实现类的方法import java.util.*;
public class TestList {
public static void main(String[] args) {
List c = new ArrayList();
c.add("aaa");
c.add("bbb");
c.add("ccc");
for(int i=0; i<c.size(); i++) {
String s = (String)c.get(i);
System.out.println(s);
}
}
}
4、创建Map实现类对象,并使用Map接口实现类的方法import java.util.*;
public class TestMap {
public static void main(String[] args) {
Map m1 = new HashMap();
Map m2 = new TreeMap();
m1.put("one",new Integer(1));
m1.put("two",new Integer(2));
m1.put("three",new Integer(3));
m2.put("A",new Integer(1));
m2.put("B",new Integer(2));
System.out.println(m1.size());
System.out.println(m1.containsKey("one"));
System.out.println(m1.containsValue(new Integer(1)));
Map m3 = new HashMap(m1);
m3.putAll(m2);
System.out.println(m3);
}
}
5、Iterator接口的使用方法
public class TestIterator {
public static void main(String []args) {
Collection c2 = new HashSet();
c2.add("aaa");
c2.add("bbb");
c2.add("ccc");
Iterator it = c2.iterator();
while( it.hasNext() ) {
String s = (String)it.next();
System.out.println(s);
}
}
}
6、使用Collections类对集合进行排序
import java.util.*;
public class TestSort {
public static void main(String[] args) {
ArrayList l1 = new ArrayList();
for(int i=0;i<=9;i++) {
l1.add("a" + i);
}
//对l1列表进行随机排序
Collections.shuffle(l1);
System.out.println(l1);
//对l1列表进行逆序排序
Collections.reverse(l1);
System.out.println(l1);
//对l1列表进行正序排序
Collections.sort(l1);
System.out.println(l1);
//用二分查找法找a5元素的位置
System.out.println(Collections.binarySearch(l1,"a5"));
}
}
7、某班级有若干学生(学生对象放在一个List中),每个学生有一个姓名属性(String)和考试成绩属性(int),某次考试结束后,每个学生都获得了一个考试成绩。
请计算并输出学生的总分和平均分。
import java.util.*;
class student {
String name;
int score;
}
public class Test {
static int all(List list){
Iterator it=list.iterator();
int sum=0;
student a;
while(it.hasNext())
{
a=(student)it.next();
sum=sum+a.score;
}
return sum;
}
static int ave(List list){
int m,n;
m=list.size();
n=all(list)/m;
return n;
}
public static void main(String[] args) {
List list=new ArrayList();
student stu1=new student();
student stu2=new student();
student stu3=new student();
="王明";
stu1.score=89;
="刘宏";
stu2.score=85;
="张三";
stu3.score=91;
list.add(stu1);
list.add(stu2);
list.add(stu3);
int i;
i=list.size();
System.out.println("学生的总分:"+all(list));
System.out.println("学生的平均分:"+ave(list));
}
}。