Java中的集合

数组与集合的比较:


数组:
1.只能添加相同类型的元素(基本数据类型 引用数据类型);
2.数组长度一旦确定 就无法更改。
集合:
1.可以添加不同类型的元素(只能是引用类型数据类型 保存基本数据类型是以自动装箱的形式 进行存储);
2.集合的长度可以更改。

需求:

创建一个数组 长度为五
(姓名 年龄)
保存三个学生
遍历学生信息

创建Student类:
public class Student {
    private String name;  //  姓名
    private String age;   //  年龄 
    //  构造方法
    public Student() {
        super();
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    //  get/set方法
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    //  重写toString()方法 专门输出对象信息
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "姓名:" + name + " 年龄:" + age;
    }
}

数组实现:
public static void main(String[] args) {
    //  创建一个保存学生的数组
    Student[] students = new Student[5];        
    //  保存学生
    students[0] = new Student("张三", 20);
    students[1] = new Student("李四", 23);
    students[2] = new Student("王麻", 21);
    //  遍历学生信息
    for(Student array : students) {
        System.out.println(array);
    }
}

集合实现:
@SuppressWarnings({"rawtypes","unchecked"})
//  注解 rawtypes 保持原有类型
//       unchecked 不检查插入数据类型
public static void main(String[] args) {
    Collection collection = new ArrayList();
    collection.add(new Student("张三", 20));
    collection.add(new Student("李四", 23));
    collection.add(new Student("王麻", 21));
    //  集合转数组(相当于 向上转型)
    Object[] array = collection.toArray();
    //  遍历数组
    for(i = 0; i < array.length; i++) {
        //  array[i]从数组中取出来是Object类型
        //  使用Student类中的方法 需要强转
        //  必须是这个类型 才能强转
        Student student = (Student)array[i];
        System.out.println(student.getName());
    }
}

错误强转方法 :

public static void main(String[] args) {
    Collection collection = new ArrayList();
    collection.add(new Student("张三", 20));
    collection.add(new Student("李四", 23));
    collection.add(new Student("王麻", 21));
    //  对象调方法 强转注意:把数组中每一个对象进行强转
    //  而不是把保存对象的容器(数组)转化
    Student[] array = (Student[])collection.toArray();
    //  遍历数组
    for (int i = 0; i < array.length; i++) {
        System.out.println(array[i].getName());
    }
}

集合方法调用:


基本方法:

public static void fun() {
    //  创建一个集合 多态的声明方法      
    Collection collection = new ArrayList();
    //  添加方法
    //  ArrayList中的add 不可能返回 失败
    //  不能返回失败 为什么还设计返回值呢?
    //  适用所有的子类 子接口
    boolean b1 = collection.add("a");
    boolean b2 = collection.add("b");
    boolean b3 = collection.add("c");
    //  当你向集合中添加基本数据类型时
    //  系统会帮进行自动装箱 把基本数据类型变成它的包装类
    boolean b4 = collection.add(10);
    boolean b5 = collection.add(true);
    //  打印集合
    System.out.println(collection.toString());
    System.out.println(b1);
    System.out.println(b2);
    System.out.println(b3);
    System.out.println(b4);
    System.out.println(b5);
    //  获取集合的长度
    System.out.println(collection.size());
    //  判断是否包含某个元素
    boolean contains = collection.contains("aa");
    System.out.println(contains);
    //  从集合中删除一个元素
    boolean remove = collection.remove("b");
    System.out.println(remove);
    //  操作的是集合本身
    System.out.println(collection);
    //  判断集合是否为空
    boolean empty = collection.isEmpty();
    System.out.println(empty);
    //  清空数组
    collection.clear();
    System.out.println(collection);
}

addAll()方法:

public static void fun() {
    Collection c1 = new ArrayList();        
    c1.add("a");
    c1.add("b");
    c1.add("c");
    Collection c2 = new ArrayList();
    c2.add("x");
    c2.add("y");
    c2.add("z");
    //  调用要测试的方法
    //  把c2集合每一个元素取出来 添加到c1集合末尾
    boolean isaddAll = c1.addAll(c2);
    System.out.println(c1);
    System.out.println(c2);
    System.out.println(isaddAll);

    //  给集合c1添加一个元素 这个元素是c2
    c1.add(c2);
    System.out.println(c1);

}

removeAll()方法:

public static void fun() {
    Collection c1 = new ArrayList();        
    c1.add("a");
    c1.add("b");
    c1.add("c");
    Collection c2 = new ArrayList();
    c2.add("a");
    c2.add("b");
    c2.add("c");
    //  这个方法 将两个集合重合元素 删除
    //  谁调用 就被删除 另一个不变
    //  只删除交集 
    boolean removeAll = c1.removeAll(c2);
    System.out.println(c1);
    System.out.println(c2);
    System.out.println(removeAll);
}

retainAll()方法:

public static void fun() {
    Collection c1 = new ArrayList();        
    c1.add("a");
    c1.add("b");
    c1.add("c");
    c1.add("d");
    Collection c2 = new ArrayList();
    c2.add("a");
    c2.add("b");
    c2.add("c");
    //  把两集合的交集取出来 谁调保存到谁那
    //  如果c1集合和c2集合 求出交集 放到c1中
    //  如果c1和原来对比 不发生变化返回false
    //  发生变化 返回true
    boolean isretainAll = c1.retainAll(c2);
    System.out.println(c1);
    System.out.println(c2);
    System.out.println(isretainAll);
}

迭代器(遍历):


简单调用:
public static void fun() {
    //  测试迭代器中的方法
    Collection collection = new ArrayList();
    collection.add("a");
    collection.add("b");
    collection.add("c");
    collection.add("d");
    //  获取集合中迭代器
    Iterator iterator = collection.iterator();
    //  先判断集合中是否有元素
    boolean isHasNext = iterator.hasNext();
    System.out.println(isHasNext);
    //  从集合中取出元素
    Object next = iterator.next();
    System.out.println(next);
}

遍历集合:
public static void fun() {
    Collection collection = new ArrayList();
    //  迭代时 有一个指针 指向集合首位置
    //  每调用一次 next()方法 这个指针向下移一格
    collection.add("a");//  ←
    collection.add("b");
    collection.add("c");
    collection.add("d");
    //  遍历集合
    Iterator iterator = collection.iterator();
    //  如果有元素 就获取 没元素 就停止循环
    while (iterator.hasNext()) {
        //  注意:迭代时 循环中只能使用一次next()方法
        System.out.println(iterator.next());
    }
}

迭代器实现打印学生信息(学生类调用上方):
public static void fun() {
    Collection collection = new ArrayList();
    collection.add(new Student("张三", 20));
    collection.add(new Student("李四", 23));
    collection.add(new Student("王麻", 21));
    //  获取集合中的迭代器
    Iterator iterator = collection.iterator();
    //  循环获取
    while (iterator.hasNext()) {
        //  获取元素
        Object next = iterator.next();
        //  强转成Student类型
        Student student = (Student)next;
        System.out.println(student.getName());
    }
}

综合举例:

/*
 *  需求:
 *  图书馆类:
 *  图书馆有名字---成员变量
 *  保存图书的容器(容器)---成员变量
 *  图书馆保存图书---方法 添加图书到图书馆的容器中
 *  打印所有图书信息---方法 遍历这个容器
 *
 *  书类:书名 作者
 */
图书馆类:
@SuppressWarnings({"rawtypes","unchecked"})
public class Library {
    private String libraryName;
    //  保存图书的容器
    //  集合必须创建集合对象 才能添加元素 可以在构造方法中创建
    private Collection books;   
    //  构造方法
    public Library() {
        super();
        //  希望调用无参的构造方法也能创建出保存的书的内容
        this.books = new ArrayList();
    }
    //  有参的构造方法 只提供一个图书馆名字的参数就可以
    public Library(String libraryName) {
        super();
        this.libraryName = libraryName;
        //  创建图书馆的容器
        this.books = new ArrayList();
    }
    //  get/set方法
    public String getLibraryName() {
        return libraryName;
    }
    public void setLibraryName(String libraryName) {
        this.libraryName = libraryName;
    }

    public Collection getBooks() {
        return books;
    }
    public void setBooks(Collection books) {
        this.books = books;
    }

    @Override
    public String toString() {
        return "图书馆名:" + libraryName + " 书名:" + books ;
    }

    //  添加书的方法
    public void addBook(Book book) {
        //  把书添加到容器中
        this.books.add(book);
    }
    //  打印图书信息
    public void print() {
        //  遍历集合
        Iterator iterator = this.books.iterator();
        while (iterator.hasNext()) {
            //  获取集合中的元素
            Object next = iterator.next();
            //  转化成书
            Book book = (Book)next;
            //  打印书
            System.out.println(book);
        }
    }
}

书类:
public class Book {
    private String bookName;
    private String writer;
    //  构造方法
    public Book() {
        super();
    }
    public Book(String bookName, String writer) {
        super();
        this.bookName = bookName;
        this.writer = writer;
    }
    //  get/set方法
    public String getBooksName() {
        return bookName;
    }
    public void setBooksName(String bookName) {
        this.bookName = bookName;
    }
    public String getWriter() {
        return writer;
    }
    public void setWriter(String writer) {
        this.writer = writer;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "书名:" + bookName + " 作者:" + writer ;
    }
}

测试类:
public class Test {
    public static void main(String[] args) {
        Library library = new Library();
        library.setLibraryName("精品图书馆");
        //  添加书
        library.addBook(new Book("西游记", "吴承恩"));
        library.addBook(new Book("水浒传", "施耐庵"));        
        library.addBook(new Book("红楼梦", "曹雪芹"));
        library.addBook(new Book("三国演义", "罗贯中"));
        //  打印图书信息
        library.print();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值