• 00077_集合


    1、集合使用的回顾

      (1)ArrayList集合存储5个int类型元素

     1 public static void main(String[] args) {
     2         ArrayList<Integer> list = new ArrayList<Integer>();
     3         list.add(111);
     4         list.add(222);
     5         list.add(333);
     6         list.add(444);
     7         list.add(555);
     8         for (int i = 0; i < list.size(); i++) {
     9             System.out.println(list.get(i));
    10         }
    11     }

      (2)ArrayList集合存储5个Person类型元素

     1 public static void main(String[] args) {
     2         ArrayList<Person> list = new ArrayList<Person>();
     3         list.add(new Person(“小强”));
     4         list.add(new Person(“老王”));
     5         list.add(new Person(“小虎”));
     6         list.add(new Person(“小泽”));
     7         list.add(new Person(“小红”));
     8         for(int i=0; i<list.size(); i++){
     9           Person p = list.get(i);
    10                 System.out.println(p);
    11          }
    12        }

      (3)集合,集合是java中提供的一种容器,可以用来存储多个数据。

      (4)数据多了,可以使用数组存放或者使用ArrayList集合进行存放数据。那么,集合和数组既然都是容器,它们有啥区别呢?
      数组的长度是固定的。集合的长度是可变的。

      (5)集合中存储的元素必须是引用类型数据。

    2、集合继承关系图

      (1)ArrayList的继承关系

      查看ArrayList类发现它继承了抽象类AbstractList同时实现接口List,而List接口又继承了Collection接口。Collection接

    interface List extends Collection {}
     public class ArrayList extends AbstractList implements List{}

      (2)集合继承体系

      我们在使用ArrayList类时,该类已经把所有抽象方法进行了重写。那么,实现Collection接口的所有子类都会进行方法重写;

      Collecton接口常用的子接口有:List接口、Set接口;

       List接口常用的子类有:ArrayList类、LinkedList类;

      Set接口常用的子类有:HashSet类、LinkedHashSet类;

      

    3、集合Collection的方法

     1  /*
     2       *  Collection接口中的方法
     3       *  是集合中所有实现类必须拥有的方法
     4       *  使用Collection接口的实现类,程序的演示
     5       *  ArrayList implements List
     6       *  List extends Collection
     7       *  方法的执行,都是实现的重写
     8       */
     9      public class CollectionDemo {
    10       public static void main(String[] args) {
    11         function_2();
    12       }
    13       
    14       
    15       /*  Collection接口方法
    16        *  Object[] toArray() 集合中的元素,转成一个数组中的元素, 集合转成数组
    17        *  返回是一个存储对象的数组, 数组存储的数据类型是Object
    18        */
    19       private static void function_2() {
    20         Collection<String> coll = new ArrayList<String>();
    21         coll.add("abc");
    22         coll.add("i");
    23         coll.add("love");
    24         coll.add("Java");
    25         coll.add("123");
    26         
    27         Object[] objs = coll.toArray();
    28         for(int i = 0 ; i < objs.length ; i++){
    29           System.out.println(objs[i]);
    30         }
    31       }
    32       /*
    33        * 学习Java中三种长度表现形式
    34        *   数组.length 属性  返回值 int
    35        *   字符串.length() 方法,返回值int
    36        *   集合.size()方法, 返回值int
    37        */
    38       
    39       /*
    40        * Collection接口方法
    41        * boolean contains(Object o) 判断对象是否存在于集合中,对象存在返回true
    42        * 方法参数是Object类型
    43        */
    44       private static void function_1() {
    45         Collection<String> coll = new ArrayList<String>();
    46         coll.add("abc");
    47         coll.add("i");
    48         coll.add("love");
    49         coll.add("Java");
    50         coll.add("123");
    51         
    52         boolean b = coll.contains("Java");
    53         System.out.println(b);
    54       }
    55 
    56 
    57       /*
    58        * Collection接口的方法
    59        * void clear() 清空集合中的所有元素
    60        * 集合容器本身依然存在
    61        */
    62       public static void function(){
    63         //接口多态的方式调用
    64         Collection<String> coll = new ArrayList<String>();
    65         coll.add("abc");
    66         coll.add("bcd");
    67         System.out.println(coll);
    68         
    69         coll.clear();
    70         
    71         System.out.println(coll);
    72         
    73       }
    74      }

    4、集合Collection的remove方法

     1 /*
     2      * Collection接口方法
     3      * boolean remove(Object o)移除集合中指定的元素
     4      */
     5     private static void function_3(){
     6       Collection<String> coll = new ArrayList<String>();
     7       coll.add("abc");
     8       coll.add("money");
     9       coll.add("love");
    10       coll.add("Java");
    11       coll.add("money");
    12       coll.add("123");  
    13       System.out.println(coll);
    14       
    15       boolean b = coll.remove("money");
    16       System.out.println(b);
    17       System.out.println(coll);
    18     }
  • 相关阅读:
    Go通过闭包避免程序运行崩溃
    占个坑
    Go多核并行
    一个计划
    python入门:模拟简单用户登录(自写)
    python入门:if、elif、else 条件语句的基本用法
    python入门:if和else的基本用法
    python入门:print打印输出的用法
    python入门:最基本的用户登录
    Python入门:Python基础笔记
  • 原文地址:https://www.cnblogs.com/gzdlh/p/8093589.html
Copyright © 2020-2023  润新知