【Java】Collection 常用功能和迭代器

这里写目录标题

Collection常用功能

/*
java.util.Collection接口
	所有单列集合的最顶层接口,里边定义了所有单列集合的共性方法;
	任意的单列集合都可以使用Collection接口中的方法

共性方法:
	public boolean add(E e); 把给定的对象添加到当前集合中
	public void clear(); 清空集合
	public boolean remove(E e); 把给定的对象在当前集合中删除
	public boolean contains(E e); 判断当前集合中是否包含给定的对象
	public boolean isEmpty(); 判断集合是否为空
	public int size(); 返回集合中元素的个数
	public Object[] toArray(); 把集合中的元素存储到数组中

*/
public class Main{
	public static void main(String[] args){
		//用多态创建集合对象,
		Collection<String> coll = new ArrayList<>();
		System.out.println(coll); // 重写了toString方法
		/*
		public boolean add(E e) 添加
		返回值是一个boolean值,一般返回true,可以不接
		*/
		coll.add("a");
		coll.add("b");
		coll.add("c");
		System.out.println(coll);
		
		//public boolean remove(E e); 把给定的对象在当前集合中删除
		coll.remove("b");
		System.out.println(coll);
		
		//public boolean contains(E e); 判断当前集合中是否包含给定的对象
		System.out.println(coll.contains("b"));
		
		//public boolean isEmpty(); 判断集合是否为空
		System.out.println(coll.isEmpty());

		//public int size(); 返回集合中元素的个数
		System.out.println(coll.size());

		//public Object[] toArray(); 把集合中的元素存储到数组中
		String[] arr = coll.toArray();
		for(String s : arr){
			System.out.println(s);
		}
		
		//public void clear(); 清空集合
		coll.clear();
		System.out.println(coll);
	}
}

迭代器

/*
java.util.Iterator接口:迭代器(对集合进行遍历)
有两个常用方法
	boolean hasNext()如果仍有元素可以迭代,返回true
		判断集合中还有没有下一个元素
	E next() 返回迭代的下一个元素
		取出集合中下一个元素
Iterator迭代器是一个接口,我们无法直接使用,需要用Iterator接口的实现对象获取实现类的方式比较特殊
collection接口中有一个方法叫iterator(),这个方法返回的就是迭代器的实现类对象
	Iterator<E> iterator() 返回在此 collection的元素上进行迭代的迭代器。

迭代器的使用步骤(重点):
	1.先使用集合中的方法 iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
	2.使用Iterator接口中的方法hasNext判断还有没有下一个元素
	3.使用Iterator接口中的方法next取出集合中的下一个元素

*/

public class Main{
    public static void main(String[] args){
        // 创建一个集合对象
        Collection<String> coll = new ArrayList<>();
        coll.add("姚明");
        coll.add("科比");
        coll.add("麦迪");
        coll.add("詹姆斯");
        coll.add("艾佛森");

        /*
            1.使用集合中的方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
               Iterator<E>接口也是有泛型的,迭代器的泛型跟着集合走,集合时什么泛型,迭代器就是什么泛型
        */
        // 多态  接口              实现类对象
        Iterator<String> it = coll.iterator();

        // 使用Iterator接口中的方法hasNext判断还有没有下一个元素
        boolean b = it.hasNext();
        System.out.println(b); // true

        // 使用Iterator接口中的方法next取出集合中的下一个元素
//        String s = it.next();
//        System.out.println(s); // 姚明
//
//        b = it.hasNext();
//        System.out.println(b);
//        s = it.next();
//        System.out.println(s);
//
//        b = it.hasNext();
//        System.out.println(b);
//        s = it.next();
//        System.out.println(s);
//
//        b = it.hasNext();
//        System.out.println(b);
//        s = it.next();
//        System.out.println(s);
//
//        b = it.hasNext();
//        System.out.println(b);
//        s = it.next();
//        System.out.println(s);
//
//        b = it.hasNext();
//        System.out.println(b); // 没有元素返回 false
//        s = it.next(); // 没有元素,抛出异常
//        System.out.println(s);

        // 可以使用循环优化,使用while循环 结束条件:hasNext返回false
        while (it.hasNext()){
            String e = it.next();
            System.out.println(e);
        }
        // 增强for循环:底层使用的也是迭代器,使用for循环的格式,简化了迭代器的书写
//        Collection<E> extend Iterator<E> 所有的单列集合都可以使用增强for循环
//          public interface Iterable<T> 实现这个接口允许对像成为forrach语句的目标
//        增强for循环用来遍历集合和数组
//        格式:
//            for(集合,数组的数据类型  变量名 : 集合,数组名){
//
//            }
        int[] arr = {1,2,3};
        for (int i: arr){
            System.out.println(i);
        }
        
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值