Collection接口

  • 特点:代表一组任意类型的对象,无序、无下标、不可重复

  • 方法:

    • boolean add(Object obj) //添加一个对象
    • boolean addAll(Collection c) //将一个集合中的所有对象添加到此集合中
    • void clear() //清空此集合中的所有对象
    • boolean contains(Object o) //检查此集合中是否包含o对象
    • boolean equals(Object o) //比较此集合是否与指定对象相等
    • boolean isEmpty() //判断此集合是否为空
    • boolean remove(Object o) //在此集合中移除o对象
    • int size() //返回此集合中的元素个数
    • Object[] toArray() //将此集合转换成数组
    //创建集合
    Collection collection = new ArrayList();
    
    //添加元素
    collection.add("apple");
    collection.add("orange");
    collection.add("pear");
    System.out.println("元素个数:" + collection.size()); //元素个数:3
    System.out.println(collection); //[apple,orange,pear]
    
    //遍历元素
    //1.增强for循环
    for(Object obj : collection){
        System.out.println(obj); //apple orange pear
    }
    //2.使用迭代器(专门用来遍历集合的一种方式)
    //hasNext(); 有没有下一个元素
    //next(); 获取下一个元素
    //remove(); 删除当前元素(迭代过程中不能使用collection的删除方法)
    Iterator it = collection.iterator();
    while(it.hasNext()){
        String s = (String)it.next();
        Systrm.out.println(s);
    }
    
    //判断
    System.out.println(collection.contains("apple")); //true
    System.out.println(collection.isEmpty()); //false
    
    //删除元素
    collection.remove("pear");
    System.out.println("删除之后:" + collection.size()); //删除之后:2
    collection.clear();
    System.out.println("删除之后:" + collection.size()); //删除之后:0
    
    //学生类
    public class Student {
    	private String name;
    	private int age;
    	
    	public Student(String name, int age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    	
    	public Student() {
    	
    	}
    
    	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;
    	}
    
    	@Override
    	public String toString() {
    		return "Student [name=" + name + ", age=" + age + "]";
    	}
        
        @Override
    	public boolean equals(Object obj){
            //判断是不是同一个对象
            if(this == obj){
                return true;
            }
            //判断是否是空
            if(obj == null){
                return false;
            }
            //判断是否是Student类型
            if(obj instanceof Student){
                Student s = (Student)obj;
                //比较属性
                if(this.name.equals(s.getName()) && this.age == s.getAge()){
                    return true;
                }
            }
            //不满足条件返回false
            return false;
        } 
    
    }
    
    //Collection使用:保存学生信息
    public class Test{
        public static void main(String[] args){
            
            //创建集合
    		Collection collection = new ArrayList();
            Student s1 = new Student("张三",20);
            Student s2 = new Student("李四",21);
            Student s3 = new Student("王五",22);
            
            //添加数据
            collection.add(s1);
            collection.add(s2);
            collection.add(s3);
            System.out.println("元素个数:" + collection.size()); //元素个数:3
            
            //遍历
            //1.增强for循环
            for(Object obj : collection){
                Student stu = (Student)obj;
                System.out.println(stu.toString());
            }
            //2.迭代器
            Iterator it = collection.iterator();
            while(it.hasNext()){
                Student s = (Student)it.next();
                System.out.println(s.toString());
            }
            
            //判断
            System.out.println(collection.contains(s1)); //true
            System.out.println(collection.contains(new Student("李四",21))); //重写equals方法后返回true
    		System.out.println(collection.isEmpty()); //false
            
            //删除数据
            collection.remove(s1);
            System.out.println("删除之后:" + collection.size()); //删除之后:2
            collection.remove(new Student("李四",21)); //重写equals方法才能实现删除
            System.out.println("删除之后:" + collection.size()); //删除之后:1
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Remote_Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值