转载:http://blog.csdn.net/last_memory/article/details/54946940
一.List包的引入
import java.util.List;
import java.util.ArrayList;
注:如果用提示(灯泡)引入的包,一定要注意是util中的,有可能错误的自动引入了java.awt.List,那么在初始化的时候会显示错误。如:List<Book> Books=new ArrayList<Book>(); 会提示你 "类型List不带有参数",一定要注意这个小细节。
二.图书馆实例:
创建一个图书馆类,实现显示所有图书,添加图书和查找图书,并且可以由读者借书,使其改变图书的借还状态。
1.创建Book类
- public class Book {
- private String Author;
- public Boolean IsBorrowed;
- private String Name;
- private Double Price;
- Book(){}
- public Book(String name,String author,double price)
- {
- Name=name;
- Author=author;
- Price=price;
- IsBorrowed=false;
- }
- public String getNmae()
- {
- return Name;
- }
-
- public String getAuthor() {
- return Author;
- }
-
- public Boolean getIsBorrowed() {
- return IsBorrowed;
- }
-
- public String getName() {
- return Name;
- }
-
- public Double getPrice() {
- return Price;
- }
-
- public void setAuthor(String Author) {
- this.Author = Author;
- }
-
- public void setIsBorrowed(Boolean IsBorrowed) {
- this.IsBorrowed = IsBorrowed;
- }
-
- public void setName(String Name) {
- this.Name = Name;
- }
-
- public void setPrice(Double Price) {
- this.Price = Price;
- }
-
- public String toString(){
- return Name+","+Author+","+Price+"元"+(IsBorrowed?"未还":"可借");
- }
-
- }
2.创建图书馆类
- import java.util.List;
- import java.util.ArrayList;
-
-
- public class Library {
- List<Book> Books;
- public Library(){
- Books=new ArrayList<Book>();
- }
- public void insertBook(Book book)
- {
- Books.add(book);
- }
- public Book[] findBook(String BookName){
- ArrayList<Book> rtn=new ArrayList<Book>();
- for(Book book:Books){
- if(book.getNmae().compareTo(BookName)==0)
- {
- rtn.add(book);
- }
- }
- Book[] rtnBook=new Book[rtn.size()];
- rtn.toArray(rtnBook);
- return rtnBook;
- }
- public Book[] GetAllBooks(){
- Book[] rtnBook=new Book[Books.size()];
- Books.toArray(rtnBook);
- return rtnBook;
- }
- }
3.创建读者类
- public class Reader {
- private String Name;
- public Reader(String name)
- {
- Name=name;
- }
- public void BorrowBook(Book book)
- {
- book.IsBorrowed=true;
- }
- public void ReturnBook(Book book)
- {
- book.IsBorrowed=false;
- }
- }
4.主函数
- import java.util.Iterator;
-
- public class Main {
-
- public static void main(String[] s) {
- Book a=new Book("格林童话","王五",16.0);
- Book b=new Book("吸血鬼日记","张三",28.6);
- Book c=new Book("三体","李四",39.8);
- Book d=new Book("格林童话","小六",42.5);
- Library libraray=new Library();
- libraray.insertBook(a);
- libraray.insertBook(b);
- libraray.Books.add(c);
- libraray.Books.add(d);
- System.out.println("获取所有图书:");
- Book[] result1=libraray.GetAllBooks();
- for(Book res:result1)
- System.out.println(res);
- System.out.println();
- System.out.println("查找指定图书:");
- Book[] result2=libraray.findBook("格林童话");
- for(Book res:result2)
- System.out.println(res);
- System.out.println();
- Reader xxx=new Reader("小明");
- xxx.BorrowBook(a);
- System.out.println("小明借书完成后,获取所有图书:");
- Book[] result3=libraray.GetAllBooks();
- for(Book res:result3)
- System.out.println(res);
- System.out.println();
- for(Book res:libraray.Books)
- if(res.getName().compareTo("三体")==0)
- res.setName("三体(第二版)");
- System.out.println("修改三体图书的属性,显示修改后的数据:");
- Iterator iter= libraray.Books.iterator();
- while(iter.hasNext())
- System.out.println(iter.next()); }}
注:对于Iterator迭代器,迭代器创建后,如果修改了list,那么需要重新创建一个新的迭代器,否则会抛出java.util.ConcurrentModificationException异常。相关详细知识请传送:
点我传送!
5.运行结果
run:
获取所有图书:
格林童话,王五,16.0元可借
吸血鬼日记,张三,28.6元可借
三体,李四,39.8元可借
格林童话,小六,42.5元可借
查找指定图书:
格林童话,王五,16.0元可借
格林童话,小六,42.5元可借
小明借书完成后,获取所有图书:
格林童话,王五,16.0元未还
吸血鬼日记,张三,28.6元可借
三体,李四,39.8元可借
格林童话,小六,42.5元可借
修改三体图书的属性,显示修改后的数据:
格林童话,王五,16.0元未还
吸血鬼日记,张三,28.6元可借
三体(第二版),李四,39.8元可借
格林童话,小六,42.5元可借