实验内容
- 定义带有泛型的集合,并对集合中的元素进行添加、删除、读取等操作。
- 在程序中使用增强的for循环分别对不同类型的集合进行遍历。
实验步骤
- 在类中定义带有泛型的List、Set及Map类型的集合
- 向集合中添加元素
- 对集合中的元素进行读取及删除操作
代码部分
以图书系统为例
//限制泛型
public class Book<T ,E,F extends Comparable > {
private T ID;//编号
private E Name;//名称
private F Prince;//价格
public Book(T id,E name,F prince){
this.ID=id;
this.Name=name;
this.Prince=prince;
}
public T getID() {
return ID;
}
public void setID(T ID) {
this.ID = ID;
}
public E getName() {
return Name;
}
public void setName(E name) {
Name = name;
}
public F getPrince() {
return Prince;
}
public void setPrince(F prince) {
Prince = prince;
}
public String toString(){
return "["+ID+","+Name+","+Prince+"]";
}
}
测试类:
import java.util.*;
public class BookTest {
public static void main(String[] args) {
//创建Book对象,传入泛型,具体类型为Integer,String,Integer
Book<Integer,String,Integer> book1=new Book<>(1,"数据结构",24);
Book<Integer,String,Integer> book2=new Book<>(2,"计算机网络",34);
Book<Integer,String,Integer> book3=new Book<>(3,"操作系统",29);
Book<Integer,String,Integer> book4=new Book<>(4,"JAVAEE架构",32);
Book<Integer,String,Integer> book5=new Book<>(5,"移动UI设计",26);
Integer id=book1.getID();
String name=book1.getName();
Integer price=book1.getPrince();
System.out.println("-------------");
System.out.println("对集合中的元素book1进行读取操作:");
System.out.println("图书编号:" + id + " 图书名称:" + name + " 图书价格:" + price);
System.out.println("************************************");
List<Book> bookList=new ArrayList<Book>();
bookList.add(book2);
bookList.add(book3);//添加
bookList.add(book4);
//Object[] obj=bookList.toArray();
/*for(Book book:bookList){
for(int i=2;i<obj.length;i++){
//Book b=(Book)obj[i];
System.out.println(book);
}
}*/
System.out.println("对list进行读取遍历:");
for(int i=0;i<bookList.size();i++)
{
System.out.println(bookList.get(i));
}
bookList.remove(book3);//删除
System.out.println("-------------");
System.out.println("对list删除元素后进行读取遍历:");
for(Book book:bookList){
System.out.println(book.getID());
System.out.println(book.getName());
System.out.println(book.getPrince());
System.out.println("-------------");
}
//定义泛型Map集合
Map<Integer,Book> books=new HashMap<Integer, Book>();
books.put(1001,book3);//将第3,4,5个Book对象存储到map中
books.put(1002,book4);
books.put(1003,book5);
System.out.println("************************************");
System.out.println("对map进行读取遍历:");
for(Integer ma:books.keySet()){
//遍历
System.out.println(ma+"----");
System.out.println(books.get(ma));
}
books.remove(1002, book4);
System.out.println("-------------");
System.out.println("对map删除元素后进行读取遍历:");
for(Integer delate:books.keySet()){
//遍历
System.out.println(delate+"----");
System.out.println(books.get(delate));
}
System.out.println("************************************");
System.out.println("对set集合的遍历:");
Set<Book> books1=new HashSet<>();
Book b7=new Book(7,"行测",17);
Book b8=new Book(8,"中国近代史",22);
Book b9=new Book(9,"网页设计与制作",37);
books1.add(b7);
books1.add(b8);
books1.add(b9);
books1.add(new Book(3,"申论",34));
for(Book bo:books1){
System.out.println(bo);
}
System.out.println("-------------");
System.out.println("对set集合进行元素删除后:");
books1.remove(b8);//删除
for(Book bo:books1){
System.out.println(bo);
}
}
}
运行结果