Java List Storage Object operation (instance explanation) __java

Source: Internet
Author: User
Tags ming

I. Introduction of the list package

Import java.util.List;

Import java.util.ArrayList;

Note: If you introduce a package with a hint (bulb), be sure to note that it is in the util, and that it is possible to automatically introduce the Java.awt.List in error, then the error will be displayed when initializing.  such as:list<book> books=new arraylist<book> (); Will prompt you "type list does not have parameter", must notice this small detail.

Two. Library Examples:

Create a library class that displays all books, adds books and finds books, and can borrow books from readers to change the status of the books.

1. Create Book Class

public class Book {private string author;//author public Boolean isborrowed;//debit status private string name;//title P Rivate Double price;//Prices Book () {}//parameterless constructor public book (String name,string author,double price) {name=n
        Ame
        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) {this.
    Price = Price;
   Public String toString () {     Return name+ "," +author+ "," +price+ "Yuan" + (isborrowed?)
    Not yet ":" "may borrow");
 }
    
}
2. Create Library class
Import java.util.List;
Import java.util.ArrayList;


public class Library {
    list<book> books;
    Public Library () {
    books=new arraylist<book> ();
            }
    public void Insertbook (books book)//method of inserting a library
    {
        books.add;
    }
    Public book[] Findbook (String bookname) {//Find a book Method
        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;//returns an array of
    } public
    book[] Getallbooks () {//Get all the book information
    book[] rtnbook=new book[ Books.size ()];
    Books.toarray (Rtnbook)//Here use array to store related book information return
    Rtnbook;
    }

3. Create a Reader class

public class Reader {
    private String Name;
    Public Reader (String name)
    {
        name=name;
    }
    public void Borrowbook (book book)
    {book
        . isborrowed=true;//Modify the loan status of the book
    } public
    void Returnbook (books)
    {bookstore
        . Isborrowed=false
    }
}

4. Main function

Import java.util.iterator;//the package public class Main {public static void main (string[] s) of the iterator that introduces iterators {book A=new book (
      "Grimm's Fairy Tales", "Harry", 16.0;
      Book B=new book ("Vampire Diaries", "John", 28.6);
      Book C=new Book ("Three Bodies", "Dick", 39.8); Book D=new Books ("Grimm's Fairy Tales", "small Six", 42.5);//Here are four library libraray=new libraries (); Initialize a library Libraray.insertbook (a
      ); Libraray.insertbook (b);//Here you can add the book Libraray by your own writing method.
      Books.add (c); Libraray.
      Books.add (d);//You can also call the Add () method adding a function System.out.println ("Get All Books:"); Book[] Result1=libraray.
       Getallbooks ();
       for (book RES:RESULT1)//This uses the for each statement to traverse the books System.out.println (res);
      System.out.println ();
      System.out.println ("Find the specified book:");
       Book[] Result2=libraray.findbook ("Grimm's Fairy Tale");//Create an array to get the returned array for (book RES:RESULT2) System.out.println (res);
      System.out.println ();
      Reader Xxx=new Reader ("xiaoming");//Create a Reader "xiaoming". Xxx.
     Borrowbook (a)//borrow "Grimm's Fairy Tales" book System.out.println ("Xiao Ming borrow the book, get all the books:"); Book[] Result3=libraray.
       Getallbooks ();
       for (book RES:RESULT3) System.out.println (res);
       System.out.println (); for (book Res:libraray.
               Books)//Modify the properties of the three-body book if (Res.getname (). CompareTo ("three Body") ==0)//Use the CompareTo () method for comparison.
 Res.setname ("Three bodies (second edition)");
 System.out.println ("Modify the attributes of the three-body book, showing the revised data:"); Iterator iter= Libraray. Books.iterator ();//Here The iterator is used to traverse while (Iter.hasnext ())//To use the Hasnext () function to judge the null System.out.println (Iter.next ()); }}
Note: For iterator iterators, after the iterator is created, if the list is modified, a new iterator needs to be recreated, otherwise a java.util.ConcurrentModificationException exception is thrown. For more information please send: dot i teleport.


5. Operating Results

Run
Get all books:
Grimm's fairy tale, Harry, 16.0 yuan to borrow
The Vampire Diary, John, 28.6 Yuan to borrow
Three body, Dick, 39.8 Yuan can borrow
Grimm's fairy tale, small six, 42.5 yuan can borrow


Find a specific book:
Grimm's fairy tale, Harry, 16.0 yuan to borrow
Grimm's fairy tale, small six, 42.5 yuan can borrow


When Xiao Ming borrows books, he gets all the books:
Grimm's fairy tale, Harry, 16.0 yuan not yet
The Vampire Diary, John, 28.6 Yuan to borrow
Three body, Dick, 39.8 Yuan can borrow
Grimm's fairy tale, small six, 42.5 yuan can borrow


Modify the properties of the three-body book to display the modified data:
Grimm's fairy tale, Harry, 16.0 yuan not yet
The Vampire Diary, John, 28.6 Yuan to borrow
Three body (second edition), Dick, 39.8 Yuan can borrow
Grimm's fairy tale, small six, 42.5 yuan can borrow

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.