杰神之Java集合的应用和案例(图书馆demo)

集合

  • 集合由来:数组操作数据的弊端
  • 数组弊端:
    1.只能添加相同类型的元素(基本数据类型和引用数据类型)
    2.长度一旦确定,就不能改变
    要添加超出数组长度个数的元素,操作复杂

  • 集合的特点:
    1.能添加不同类型的元素
    注意:集合中只能添加引用数据类型(对象类型)
    2.长度可变

集合框架体系

这里写图片描述

集合的基本方法

// 创建集合 多态的声明方式
Collection collection = new ArrayList();
// 添加方法
// 向集合中添加基本数据类型的时候
// 系统会自动装箱,把基本数据类型变成该数据类型的包装类
// ArrayList 中的 add 方法不可能返回失败
// 因为没有写返回 false 的判断 只有 true
// 思考:不能返回失败,为什么还要设计返回值呢?(思想)
// 要适用所有的子类,子接口
boolean add = collection.add("a");
boolean add2 = collection.add("b");
boolean add3 = collection.add("c");
boolean add4 = collection.add(10);
boolean add5 = collection.add(true);
// 打印集合
System.out.println(collection.toString());
// 获取集合长度
int size = collection.size();
System.out.println(size);
// 判断包含
boolean contains = collection.contains(10);
System.out.println(contains);
// 删除
boolean remove = collection.remove(true);
System.out.println(remove);
// 操作的是集合本身
System.out.println(collection);
// 判断集合是否为空
boolean empty = collection.isEmpty();
System.out.println(empty);
// 清空数组
collection.clear();
System.out.println(collection);
打印字符串
private static void function() {
    Collection collection = new ArrayList();
    collection.add("a");
    collection.add("b");
    collection.add("c");
    collection.add("d");
    // 遍历集合中的每个元素(集合转数组)
    Object[] array = collection.toArray();
    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i]);
    }
}
创建一个集合,加入三个学生,从集合中取出学生姓名

private static void function() {
    Collection collection = new ArrayList();
    collection.add(new Student("学生1", 18));
    collection.add(new Student("学生2", 18));
    collection.add(new Student("学生3", 18));
    // 集合转数组(相当于有一个向上转型的操作)
    // 对象调方法(强转注意:把数组中每一个对象进行强转,
    而不是把保存对象的容器(数组)转化)
    Object[] array = collection.toArray();
    // 遍历数组
    for (int i = 0; i < array.length; i++) {
        // array[i] 直接从数组中取出来是 Object 类型
        // 要想使用 Student 类中的方法,需要强壮
        // 注意:必须是这个类型,才能强转成这个类型!
        Student student = (Student)array[i];
        System.out.println(student.getName());
    }
}

错误的强转方式
public static void function() {
    Collection collection = new ArrayList();
    collection.add(new Student("学生1", 18));
    collection.add(new Student("学生2", 18));
    collection.add(new Student("学生3", 18));
    Student[] array = (Student[])collection.toArray();
    for (Student student : array) {
        System.out.println(student);
    }
}
addAll
private static void function() {
    Collection collection = new ArrayList();
    collection.add("a");
    collection.add("b");
    collection.add("c");
    Collection collection2 = new ArrayList();
    collection2.add("x");
    collection2.add("y");
    collection2.add("z");
    // 给集合 collection 添加一个元素,这个元素是 collection2
    // collection.add(collection2);
    // System.out.println(collection);

    // 调用要测试的方法
    collection.addAll(collection2);
    // 查看调用后效果
    // 把 collection2集合中每一个元素取出来添加到 collection 集合中
    System.out.println("c:" + collection);
    System.out.println("c2:" + collection2);
}
removeAll
private static void function() {
    Collection collection = new ArrayList();
    collection.add("a");
    collection.add("b");
    collection.add("c");
    Collection collection2 = new ArrayList();
    collection2.add("x");
    collection2.add("y");
    collection2.add("z");
    // 集合调用方法,将该集合中两个集合相同的所有元素删除
    collection.removeAll(collection2);
    System.out.println("c:" + collection);
    System.out.println("c2:" + collection2);
}
retainAll
private static void function() {
    Collection collection = new ArrayList();
    collection.add("a");
    collection.add("b");
    collection.add("c");
    Collection collection2 = new ArrayList();
    collection2.add("x");
    collection2.add("y");
    collection2.add("z");
    // 集合调用方法,把两个集合的交集取出来,保存到该集合,其他元素删除
    // 如果 collection 集合和 collection2 求出交集放到 collection 中
    // 如果 collection 和原来对比,发生变化返回 true
    // 不发生变化返回 false
    collection.retainAll(collection2);
    System.out.println("c:" + collection);
    System.out.println("c2:" + collection2);
}

迭代器(集合的遍历)

// 测试迭代器中的方法
Collection collection = new ArrayList();
collection.add("a");
collection.add("b");
collection.add("c");
// 获取集合中的迭代器
Iterator iterator = collection.iterator();
// 先判断集合中是否有元素
boolean hasNext = iterator.hasNext();
System.out.println(hasNext);
// 从集合中取出元素
Object next = iterator.next();
System.out.println(next);
遍历集合
private static void function() {
    Collection collection = new ArrayList();
    // 迭代时,有一个指针,指向集合首位置
    // 每调用一次 next 方法,指针就向下移一格
    collection.add("a");
    collection.add("b");
    collection.add("c");
    // 遍历集合
    Iterator iterator = collection.iterator();
    // 如果有元素就获取没元素就停止循环
    while (iterator.hasNext()) {
        // 注意:迭代时循环中只能使用一次 next() 方法
        System.out.println(iterator.next());
    }
}
创建一个集合
保存3学生
使用迭代器遍历,打印学生姓名

private static void function() {
    Collection collection = new ArrayList();
    collection.add(new Student("学生1", 18));
    collection.add(new Student("学生2", 18));
    collection.add(new Student("学生3", 18));
    // 获取集合中迭代器
    Iterator iterator = collection.iterator();
    // 循环获取对象
    while (iterator.hasNext()) {
        // 获取元素
        Object next = iterator.next();
        // 强转成 Student 类型
        Student student = (Student) next;
        // 打印姓名
        System.out.println(student.getName());
    }
}

集合和迭代器应用案例 需求:

创建图书管类
图书馆有名字
图书馆能保存图书
打印所有图书的信息
书类
书名
作者

Library类


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

@SuppressWarnings({"rawtypes","unchecked"})
public class Library {
    private String libraryName;
    private Collection collection=new ArrayList();

    public Library() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Library(String libraryName) {
        super();
        this.libraryName = libraryName;
    }

    public String getLibraryName() {
        return libraryName;
    }

    public void setLibraryName(String libraryName) {
        this.libraryName = libraryName;
    }

    public Collection getCollection() {
        return collection;
    }

    public void setCollection(Collection collection) {
        this.collection = collection;
    }

    public void addBook(Book book) {
        collection.add(book);
    }
    public void printBook() {
        System.out.println(this.libraryName + "收录了:");
        Iterator iterator = collection.iterator();
        for (int i = 0; i < collection.size(); i++) {
            boolean isHasNext = iterator.hasNext();
            if (isHasNext == true) {
                Book book = (Book) iterator.next();
                System.out.println(book.toString());
            }
        }
    }
}

Book类


public class Book {
    private String kind;
    private String bookName;
    private String author;

    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Book(String kind, String bookName, String author) {
        super();
        this.kind = kind;
        this.bookName = bookName;
        this.author = author;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "书类:" + kind +"\t"+ "  书名:" + bookName +"\t"+ "作者:" + author+"\t";
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        Library library =new Library("新华书店");
        library.addBook(new Book("小说","西游记","吴承恩"));
        library.addBook(new Book("小说","水浒传","罗贯中"));
        library.addBook(new Book("小说","三国演义","施耐庵"));
        library.addBook(new Book("小说","红楼梦","曹雪芹"));
        library.printBook();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值