软件设计实验18:迭代器模式
实验18:迭代器模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解迭代器模式的动机,掌握该模式的结构;
2、能够利用迭代器模式解决实际问题。
[实验任务一]:JAVA和C++常见数据结构迭代器的使用
信1305班共44名同学,每名同学都有姓名,学号和年龄等属性,分别使用JAVA内置迭代器和C++中标准模板库(STL)实现对同学信息的遍历,要求按照学号从小到大和从大到小两种次序输出学生信息。
实验要求:
1. 搜集并掌握JAVA和C++中常见的数据结构和迭代器的使用方法,例如,vector, list, map和set等;
Java:
数据结构:
1.ArrayList:
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
2.LinkedList:
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("A");
linkedList.add("B");
linkedList.add("C");
3.HashMap:
HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("one", 1);
hashMap.put("two", 2);
hashMap.put("three", 3);
4.HashSet:
HashSet<String> hashSet = new HashSet<>();
hashSet.add("apple");
hashSet.add("orange");
hashSet.add("banana");
迭代器:
5.Iterator (for ArrayList, LinkedList):
Iterator<Integer> iterator = arrayList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
6.HashMap EntrySet Iterator:
for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
7.HashSet Iterator:
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
C++:
数据结构:
8.Vector:
#include <vector>
std::vector<int> vectorInt = {1, 2, 3};
9.List:
#include <list>
std::list<std::string> listString = {"A", "B", "C"};
10.Map:
#include <map>
std::map<std::string, int> mapInt = {{"one", 1}, {"two", 2}, {"three", 3}};
11.Set:
#include <set>
std::set<std::string> setString = {"apple", "orange", "banana"};
迭代器:
12.Iterator (for Vector, List):
for (auto it = vectorInt.begin(); it != vectorInt.end(); ++it) {
std::cout << *it << std::endl;
}
13.Map Iterator:
for (auto it = mapInt.begin(); it != mapInt.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
14.Set Iterator:
for (auto it = setString.begin(); it != setString.end(); ++it) {
std::cout << *it << std::endl;
}
需要注意的是,C++11及以后版本引入了更方便的范围-based for 循环,可以简化上述的迭代器使用。例如:
// 使用范围-based for 循环遍历 Vector
for (const auto& element : vectorInt) {
std::cout << element << std::endl;
}
// 使用范围-based for 循环遍历 Map
for (const auto& entry : mapInt) {
std::cout << entry.first << ": " << entry.second << std::endl;
}
// 使用范围-based for 循环遍历 Set
for (const auto& element : setString) {
std::cout << element << std::endl;
}
2. 提交源代码;
public class Student2 implements Comparable<Student2>{
private int studentid;
private String name;
private int age;
public Student2(int studentid, String name, int age) {
super();
this.studentid = studentid;
this.name = name;
this.age = age;
}
// 三个返回结果都要写出来
public int compareTo(Student2 o) {
if(this.studentid < o.studentid){
return -1;
}else if(this.studentid > o.studentid){
return 1;
}else {
return 0;
}
}
@Override
public String toString(){
return "姓名: " + this.name + ". 学号: " + this.studentid + ". 年龄: " + this.age;
}
}
public class Student implements Comparable<Student>{
private int studentid;
private String name;
private int age;
public Student(int studentid, String name, int age) {
super();
this.studentid = studentid;
this.name = name;
this.age = age;
}
// 三个返回结果都要写出来
public int compareTo(Student o) {
if(this.studentid > o.studentid){
return -1;
}else if(this.studentid < o.studentid){
return 1;
}else {
return 0;
}
}
@Override
public String toString(){
return "姓名: " + this.name + ". 学号: " + this.studentid + ". 年龄: " + this.age;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Client {
public static void main(String[] args) {
Student s1 = new Student(20213825, "杰克逊", 20);
Student s2 = new Student(20213999, "孙文亮", 20);
Student s3 = new Student(20216654, "阿giao", 20);
Student s4 = new Student(20213367, "丽丽", 20);
Student s5 = new Student(20213396, "全险半挂", 20);
Student s6 = new Student(20213396, "农村国宝", 20);
Student2 s7 = new Student2(20213825, "杰克逊", 20);
Student2 s8 = new Student2(20213999, "孙文亮", 20);
Student2 s9 = new Student2(20216654, "阿giao", 20);
Student2 s10 = new Student2(20213367, "丽丽", 20);
Student2 s11 = new Student2(20213396, "全险半挂", 20);
Student2 s12 = new Student2(20213396, "农村国宝", 20);
List<Student> list = new ArrayList<Student>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
list.add(s6);
Collections.sort(list);
System.out.println("按照学号从大到小输出: ");
for(Student stu : list){
System.out.println(stu.toString());
}
System.out.println("---------------------------------------");
List<Student2> list2 = new ArrayList<Student2>();
list2.add(s7);
list2.add(s8);
list2.add(s9);
list2.add(s10);
list2.add(s11);
list2.add(s12);
Collections.sort(list2);
System.out.println("按照学号从小到大输出: ");
for(Student2 stu : list2){
System.out.println(stu.toString());
}
}
}
3. 注意编程规范。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~