集合Collection父接口

本文介绍了Java中的Collection接口,包括其特点、常用方法以及如何进行元素的添加、删除、遍历和判断。通过示例代码展示了如何使用ArrayList实现Collection接口,以及如何保存和操作学生信息。
摘要由CSDN通过智能技术生成

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()//将此集合转换成数组

示例代码如下

package com.collection;

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

public class Demo01 {

    /**
     * Collection接口的使用
     * (1)添加元素
     * (2)删除元素
     * (3)遍历元素
     * (4)判断
     * @author qianchang
     *
     */
    public static void main(String[] args) {
        //创建集合
        Collection collection=new ArrayList();
        //1,添加元素
        collection.add("苹果");
        collection.add("橘子");
        collection.add("芒果");
        System.out.println("元素个数"+collection.size());
        System.out.println(collection);
        //2,删除元素
        //collection.remove("橘子");
        //collection.clear();
        System.out.println("删除之后"+collection.size());
        //3,遍历元素
        //(1)使用增强for循环
        System.out.println("==============================");
        for (Object object:collection)
        {
            System.out.println(object);
        }
        //(2)使用迭代器(迭代器专门用来遍历集合的一种方式)
        //hasNext();有没有下一个元素
        //next();获取下一个元素
        //remove();删除当前元素
        System.out.println("==================================");
        Iterator it =collection.iterator();
        while(it.hasNext()){
            String s =(String)it.next();
            System.out.println(s);
            //collection.remove(s);无法使用,会报并发修改错误
            //System.out.println(collection.contains("苹果"));,可以使用
            //it.remove();
        }
        System.out.println("元素个数"+collection.size());
        //4,判断
        System.out.println(collection.contains("苹果"));
        System.out.println(collection.isEmpty());

    }
}
package com.collection;

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

/**
 * Collection的使用:保存学生信息
 * @author 墨染纤尘写浮生
 */
public class Demo02 {
    public static void main(String[] args) {
        //新建Collection对象
        Collection collection=new ArrayList();
        Student s1=new Student("张三",20);
        Student s2=new Student("李四",21);
        Student s3=new Student("王五",19);
        //1,添加数据,可添加重复的数据
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println("元素个数"+collection.size());
        System.out.println(collection.toString());
        //2,删除
        //collection.remove(s1);
        //collection.remove(new Student("王五",19));
        //collection.clear();
        //System.out.println("删除之后"+collection.size());
        //3,遍历
        //(1)增强for循环
        System.out.println("================================");
        for (Object object:collection)
        {
            Student s =(Student) object;
            System.out.println(s.toString());
        }
        //(2)迭代器;迭代过程种不能使用collection的删除方法
        System.out.println("====================");
        Iterator it = collection.iterator();
        while(it.hasNext()){
            Student s=(Student) it.next();
            System.out.println(s.toString());
        }
        //4,判断
        System.out.println(collection.contains(s1));
        System.out.println(collection.isEmpty());

    }
}
package com.collection;

/**
 * 学生类
 * @author 墨染纤尘写浮生
 */
public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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 +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值