zoukankan      html  css  js  c++  java
  • Java进阶--Collection集合

    说明:本文及后面博客Java中的集合部分的内容为黑马程序员(传智播客)的学习笔记,课程相关内容版权属黑马程序员(传智播客)。
    哔哩哔哩视频链接:https://www.bilibili.com/video/BV1Ak4y1o7y8?from=search&seid=2916563301342713061

    Collection集合


            
    image.png 

    1.1 集合概述


    我们已经学习过并使用过集合ArrayList ,那么集合到底是什么呢?

    • 集合:集合是java中提供的一种容器,可以用来存储多个数据


    集合和数组既然都是容器,它们有啥区别呢?

    • 数组的长度是固定的。集合的长度是可变的。
      • int[] arr = new int[10];
      • Student[] stu = new Student[3];
      • ArrayList
    • 数组中存储的是同一类型的元素,可以存储基本数据类型值。集合存储的都是对象。而且对象的类型可以不一致。在开发中一般当对象多的时候,使用集合进行存储。
    int[] arr = new int[10];
    Student[] stu = new Student[3];
    ArrayList<Student><String><Integer>
    

    1.2  集合框架

    image.png

    1.3 Collection 常用功能


    Collection是所有单列集合的父接口,因此在Collection中定义了单列集合(List和Set)通用的一些方法,这些方法可用于操作所有的单列集合。方法如下:

    • 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(): 把集合中的元素,存储到数组中。


    方法演示:

    package 集合和泛型.Collection;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashSet;
    
    /**
     * 在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 Demo01Collection {
        public static void main(String[] args) {
            demo01();
        }
    
        public static void demo01(){
            // 创建集合对象,可以使用多态
            // Collection<String> coll = new ArrayList<>();
            Collection<String> coll = new HashSet<>();
            System.out.println(coll);
            // add 方法
            boolean b1 = coll.add("张三");
            System.out.println(b1);
            System.out.println(coll);
            coll.add("李四");
            coll.add("李四");     // HashSet会自动去重
            coll.add("王五");
            coll.add("赵六");
            coll.add("田七");
            System.out.println(coll);
    
            // remove方法 : 存在元素会删除元素返回True,不存在删除失败返回False
            boolean b2 = coll.remove("赵六");
            System.out.println(b2);
            System.out.println(coll);
            boolean b3 = coll.remove("赵四");
            System.out.println(b3);
    
            // contains() 方法  是否包含某个元素,包含True,否则false
            boolean b4 = coll.contains("张三");
            System.out.println(b4);
            boolean b5 = coll.contains("张六");
            System.out.println(b5);
    
            // isEmpty()方法   :为空True,不为空True
            boolean b6 = coll.isEmpty();
            System.out.println(b6);
    
            // size()方法
            System.out.println(coll.size());
    
            //toArray(): 把集合中的元素,存储到数组中。
            Object [] arr = coll.toArray();
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
    
            //public void clear() :清空集合中所有的元素。
            coll.clear();
            System.out.println(coll);
            System.out.println(coll.isEmpty());
        }
    }
    
    

    tips: 有关Collection中的方法可不止上面这些,其他方法可以自行查看API学习。

  • 相关阅读:
    实验3
    实验2
    实验1
    阿里云服务器搭建Halo博客系统
    移动开发技术(三)
    在服务器上部署Anki-Server
    软件测试技术(二)
    函数相关定理
    移动开发技术(一)
    高等数学函数求导
  • 原文地址:https://www.cnblogs.com/sinlearn/p/13371702.html
Copyright © 2011-2022 走看看