首页 > 其他分享 >Day35:Collection

Day35:Collection

时间:2022-12-09 00:13:15浏览次数:33  
标签:Object Day35 System Collection 集合 boolean println out

集合

1.1 概念

存储对象的容器。定义了对多个对象进行操作的常用方法,比如添加、删除等方法

  • 数组一旦创建其长度不可变,而集合长度是不固定的;

  • 数组可以存储基本类型和引用类型,集合只能存储引用类型

1.2 Collection

Collection代表一组对象,称之为“集合”,是一个父接口,其有两个子接口List以及Set

List接口的特点:有序、有下标、元素可重复;其实现类有ArrayListLinkedListVector;

Set接口的特点:无序、无下标、元素不可重复;其实现类有HashSetTreeSet;子接口SortedSet

Collection
特点:代表一组任意类型的对象,无序,无下标,不能重复。

方法:

方法名 说明
boolean add(Object o) 添加一个对象
boolean addAll(Collection c) 添加一个集合到此集合中
void clear() 清理此集合中的对象
boolean contains(Object o) 判断一个对象是否在此集合中
boolean equals(Object o) 比较此集合是否与指定对象相等
boolean isEmpty() 判断此集合是否为空
boolean remove(Object o) 移除此集合一个对象并返回布尔值
int size() 返回集合的元素个数
Object [] toArray() 将此集合转换成数组

接下来在代码中演示方法

public class Test{
    public static void main(String[] args){
        //创建Collection对象
        Collection c=new ArrayList();//Collection为一个接口,不能直接实例化,通过其实现类来实例化

        //boolean add(Object o);   在此集合内增加一个对象
        System.out.println("==================add================");
        c.add("苹果");
        c.add("香蕉");
        c.add("哈密瓜");
        System.out.println(c);

        //boolean remove(Object o); 移除集合内的某一对象
        System.out.println("==================remove================");
        c.remove("苹果");
        System.out.println(c);

        //int size();               返回此集合的元素(对象)个数
        System.out.println("==================size================");
        System.out.println(c.size());

        //void clear();             清除此集合内的所有元素
        /*c.clear();
        System.out.println(c);
        System.out.println(c.size);*/  //注释此方法,下面还有方法要演示

        //遍历集合元素
        //方法一:增强for循环
        System.out.println("==================foreach================");
        for(Object o:c){
            String s=(String)o;//因为此处遍历的是Object类型,需要强制转换为我们需要的类型
            System.out.println(s.toString());
        }
        //方法二:使用迭代器iterator——————专用遍历集合的方法————返回一个Iterator类型的对象
        //Iterator接口的方法:
        //hasNext();检测是否有下一个元素
        //next();获取刚才检测的元素;返回一个Object类型
        //remove();删除元素
        System.out.println("==================iterator================");
        Iterator i=c.iterator();//调用接口的遍历方法
        while(i.hasNext()){
            String s=(String)i.next();//next();方法返回的是Object类型,我们需要将其转换为元素类型
            System.out.println(s);
            /*
            我们在迭代器中不能使用集合的移除方法,否则会报错并发修改异常
            c.remove(s);
            */
            //使用迭代器的remove方法进行移除元素
            i.remove();
        }
        System.out.println(c);

        //判断方法
        //boolean contains(Object o);判断此集合是否含有元素o
        System.out.println("==================contains================");
        System.out.println(c.contains("榴莲"));
        //boolean isEmpty();判断此集合是否为空集
        System.out.println("==================isEmpty================");
        System.out.println(c.isEmpty());
    }
}
==================add================
[苹果, 香蕉, 哈密瓜]
==================remove================
[香蕉, 哈密瓜]
==================size================
2
==================foreach================
香蕉
哈密瓜
==================iterator================
香蕉
哈密瓜
[]
==================contains================
false
==================isEmpty================
true

标签:Object,Day35,System,Collection,集合,boolean,println,out
From: https://www.cnblogs.com/CQliuwei/p/16967781.html

相关文章