Collection接口

Collection接口实现类的特点

public interface Collection<E> extends iterable<E>

(1)collection实现子类可以存放多个元素,每个元素可以是Object

(2)有些collection的实现类,可以存放重复的元素,有些不可以

(3)有些collection的实现类,有些是有序的(List),有些是无序的(Set)

(4)Collection接口没有直接的实现子类,是通过它的子接口Set和List来实现的

  • 常用方法

  • (1)add:添加单个元素

    (2)remove:删除指定元素

    (3)contains:查找元素是否存在

    (4)size:获取元素个数

    (5)isEmpty:判断是否为空

    (6)clear:清空

    (7)addAll:添加多个元素

    (8)containsAll:查找多个元素是否存在

    (9)removeAll:删除多个元素

import java.util.ArrayList;

public class CollectionMethod {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();

        //添加单个元素
        arrayList.add("jack");
        arrayList.add(10);  //相当于arrayList.add(new Integer(10))
        arrayList.add(true);
        System.out.println("arrayList = " + arrayList);

        //删除元素
        arrayList.remove(0);    //删除第一个元素
        arrayList.remove("jack");   //删除指定元素

        //查找元素是否存在
        System.out.println(arrayList.contains("jack"));

        //返回元素个数
        System.out.println(arrayList.size());

        //判断是否为空
        System.out.println(arrayList.isEmpty());

        //清空
        arrayList.clear();
        System.out.println("arrayList = " + arrayList);

        //添加多个元素
        ArrayList arrayList1 = new ArrayList();
        arrayList1.add("红楼梦");
        arrayList1.add("三国演义");
        arrayList.addAll(arrayList1);
        System.out.println("list = " + arrayList);

        //查找多个元素是否存在
        System.out.println(arrayList.containsAll(arrayList1));

        //删除多个元素
        arrayList.removeAll(arrayList1);
    }
}

接口遍历方式1:使用Iterator(迭代器)

基本介绍:Iterator<E>

(1)Iterator对象称为迭代器,主要用于遍历Collection集合中的元素

(2)所有实现了Collection接口的集合类都有一个iterator()方法,用于返回一个实现Iterator接口的对象,即可以返回一个迭代器

(3)Iterator仅用于遍历集合,Iterator本身并不存放对象

迭代器的执行原理:

Iterator iterator= coll.iterator();  //得到一个集合的迭代器

//hasNext();  判断是否还有下一个元素

while(iterator.hasNext() ){

//next();  1.指针下移 2.将下移以后集合位置上的元素返回

System.out.println(iterator.next() );

}

注意:在调用iterator.next()方法之前必须要调用iterator.hasNext()进行检测。若不调用,且下一条记录无效,直接调用iterator.next()会抛出NoSuchElementException异常

import java.util.Collection;

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

public class CollectionIterator {
    public static void main(String[] args) {
        Collection col = (Collection) new ArrayList();

        col.add(new Books("三国演义","罗贯中",10.1));
        col.add(new Books("红楼梦","曹雪芹",34.6));
        //System.out.println("col=" + col);

        //1.先得到col对应的迭代器
        Iterator iterator = col.iterator();
        //2.使用while循环遍历 循环快捷键 itit
        while (iterator.hasNext()) {    //判断是否还有数据
            Object object = iterator.next();  //返回下一个元素,类型是Object
            System.out.println("object=" + object);
            //编译类型是object,运行类型取决与实际存放的对象
        }
    }
}

class Books {
    private String name;
    private String author;
    private double price;

    public Books(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Books{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}
  • 接口遍历方式2:增强for循环

  • for(元素类型 元素名 :集合名或数组名){

    访问元素

    }

  • import java.util.ArrayList;
    import java.util.Collection;
    
    public class CollectionFor {
        public static void main(String[] args) {
            Collection col = (Collection) new ArrayList();
    
            col.add(new Books("三国演义","罗贯中",10.1));
            col.add(new Books("红楼梦","曹雪芹",34.6));
    
            //使用增强for循环
            for(Object book : col) {
                System.out.println("book=" + book);
            }
        }
    }

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值