Java基础01-集合1、泛型

集合、泛型

第一章:集合1

1. 什么是集合

  • 定义:在Java中,集合是一种可以存储多个数据的容器。

  • 代码:

    1       ArrayList<String> list = new ArrayList<>();
    2       list.add("张三");
    3       list.add("王五");
    4       list.add("赵六");
    5       System.out.println(list); // [张三, 王五, 赵六]

2. 集合和数组的区别

  • 区别:数组的长度是固定的,集合的长度是可变的

  • 代码:

     1       String[]strs = new String[2];
     2       strs[0] = "张三";
     3       strs[1] = "李四";
     4       // strs[2] = "赵六"; // 运行时报错java.lang.ArrayIndexOutOfBoundsException
     5  6       ArrayList<String> list = new ArrayList<>();
     7       System.out.println(list.size()); // 0
     8       list.add("张三");
     9       list.add("王五");
    10       list.add("赵六");
    11       System.out.println(list.size());  // 3

3. 集合框架Collection

  • 集合按照其存储结构可以分为两大类,本篇先学习Collection

  • Collection集合介绍

    • 单列集合类的根接口

    • 有两个子接口

      • java.util.List;特点是:内容可重复,有序

      • java.util.Set; 特点是:内容不可重复,无序

    • 定义了单例集合最共性的方法。

  • 图解Collection框架结构

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

    • 代码:

            List<String> list = new ArrayList<>();
            // 添加元素
            list.add("张三");
            list.add("李四");
            System.out.println(list);
            // 移除元素
            list.remove("张三");
            System.out.println(list);
            // 判断集合中是否包含某个元素
            boolean isHas = list.contains("张三");
            System.out.println(isHas); // false
            // 判断当前集合是否为空
            boolean isEmpty = list.isEmpty();
            System.out.println(isEmpty);
            // 清空元素
            list.clear();
            System.out.println(list);
            // 集合的长度
            System.out.println(list.size());
            // 集合中的元素存储到一个数组中
            Object[]s =  list.toArray();

4. 遍历集合

  • Iterator方式

    • 介绍

      ​ Iterator,是一个迭代器接口。Collection中的成员方法iterator()被调用后,会返回一个Iterator对象。利用这个对象可以实现遍历集合。

      ​ 如何遍历呢?在取元素之前先要判断集合中有没有元素,如果有,就把这个元素取出来,继续在判断,如果还有就再取出出来。一直把集合中的所有元素全部取出。这种取出方式专业术语称为迭代。

    • Iterator对象的成员方法:

      • hasNext(); 检测集合中是否存在下一个元素

      • next(); 找到并获取下一个元素

    • 迭代器的原理

      ​ 在调用Iterator的next方法之前,迭代器的索引位于第一个元素之前,不指向任何元素,当第一次调用迭代器的next方法后,迭代器的索引会向后移动一位,指向第一个元素并将该元素返回,当再次调用next方法时,迭代器的索引会指向第二个元素并将该元素返回,依此类推,直到hasNext方法返回false,表示到达了集合的末尾,终止对元素的遍历。

    • 代码:

       1       List<String> list = new ArrayList<>();
       2       list.add("张三");
       3       list.add("李四");
       4       list.add("王五");
       5       // 得到一个迭代器对象
       6       Iterator<String> it = list.iterator();
       7       while (it.hasNext()) {
       8         String str = it.next();
       9         System.out.println(str);
      10       }
  • 增强for

    • 介绍

      ​ 增强for循环(也称for each循环)是JDK1.5以后出来的一个高级for循环,专门用来遍历数组和集合的。它的内部原理其实是个Iterator迭代器,所以在遍历的过程中,不能对集合中的元素进行增删操作。

    • 使用格式

        
      for(元素的数据类型  变量 : Collection集合or数组){
      //写操作代码
      }
    • 代码:

        
      for (String name:list) {
       System.out.println(name);
      }

第二章:泛型

1. 什么是泛型

​ 泛型,未知的类型,可以在类或方法中预支地使用未知的类型。

2. 泛型的好处

​ 可以避免类型转换的麻烦。

3. 定义和使用含有泛型的类

  • 格式修饰符 class 类名<代表泛型的变量> { }

  • 使用泛型:在创建对象的使用。也就是在创建对象时确定泛型的类型。

  • 代码:

     1  
     2   public class ArrayList<E> {
     3       public boolean add(E e){ }
     4       ...
     5   }
     6  7   public class MainDemo{
     8     public static void main(String[] args) {
     9       ArrayList<String> list = new ArrayList<>();
    10       list.add("张三");
    11     }
    12   }

4. 定义和使用含有泛型的方法

  • 格式:修饰符 <代表泛型的变量> 返回值类型 方法名(参数){ }

  • 代码:

    1   
    2   public static void main(String[] args) {
    3      methon1("张三");
    4   }
    5   public static  <M> void methon1(M m){
    6      System.out.println(m);
    7   }

5. 定义和使用含有泛型的接口

  • 格式:修饰符 interface接口名<代表泛型的变量>

  • 代码:

     1  
     2   public interface TestInterface<T> {
     3     public abstract void show(T t);
     4   }
     5  6   public class<T> TestImpl implements TestInterface<T> {
     7     @Override
     8     public T void show(T o) {
     9       System.out.println(o);
    10     }
    11   }
    12 13   public class Main01 {
    14     public static void main(String[] args) {
    15       TestImpl<String> t = new TestImpl<>();
    16       t.show("我的信息");
    17 18     }
    19     
    20   }

6. 泛型通配符

  • 介绍

    不知道使用什么类型来接收的时候,此时可以使用?,?表示未知通配符。

    此时只能接受数据,不能往该集合中存储数据。

  • 代码:

     1  public static void main(String[] args) {
     2       ArrayList<String> list1 = new ArrayList<>();
     3       list1.add("张三");
     4       list1.add("李四");
     5       ArrayList<Integer> list2 = new ArrayList<>();
     6       list2.add(1);
     7       list2.add(2);
     8       print(list1);
     9       print(list2);
    10     }
    11     // 定义一个方法打印集合,不确定未来集合中的元素类型,<>中不能写入Object中,因为泛型中不存在继承关系。
    12     public static void print(ArrayList<?> list){
    13       System.out.println(list);
    14     }

     

7. 泛型通配符受限使用

  • 受限1类型名称 <? extends 类 > 对象名称

    • 只能接收该类型或该类型的子类

  • 受限2类型名称 <? super 类 > 对象名称

    • 只能接收该类型或该类型的父类

  • 代码

     1   public static void main(String[] args) {
     2       Collection<Integer> list1 = new ArrayList<Integer>();
     3       Collection<String> list2 = new ArrayList<String>();
     4       Collection<Number> list3 = new ArrayList<Number>();
     5       Collection<Object> list4 = new ArrayList<Object>();
     6       
     7       getElement(list1);
     8       getElement(list2);//报错
     9       getElement(list3);
    10       getElement(list4);//报错
    11     
    12       getElement2(list1);//报错
    13       getElement2(list2);//报错
    14       getElement2(list3);
    15       getElement2(list4);
    16     
    17   }
    18   // 泛型的上限:此时的泛型?,必须是Number类型或者Number类型的子类
    19   public static void getElement1(Collection<? extends Number> coll){}
    20   // 泛型的下限:此时的泛型?,必须是Number类型或者Number类型的父类
    21   public static void getElement2(Collection<? super Number> coll){}

第三章:综合案例斗地主

  • 需求:

    • 实现发牌功能,并且展示底牌。

  • 代码:

     1   package it.leilei.cn.demo05;
     2  3   import java.util.ArrayList;
     4   import java.util.Collection;
     5   import java.util.Collections;
     6  7   public class Main01 {
     8     public static void main(String[] args) {
     9       // 1. 准备牌
    10       // 牌的花色
    11       String[]colors = {"♥","♠","♣","♦"};
    12       // 牌的数字
    13       String[]nums = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
    14       // 2. 组装装牌
    15       ArrayList<String> poker = new ArrayList<>();
    16       for (String num:nums) {
    17         for (String color:colors) {
    18           poker.add(num + color);
    19         }
    20       }
    21       poker.add("大王");
    22       poker.add("小王");
    23 24       // 3. 发牌
    25       ArrayList<String> list1 = new ArrayList<>();
    26       ArrayList<String> list2 = new ArrayList<>();
    27       ArrayList<String> list3 = new ArrayList<>();
    28       ArrayList<String> diPai = new ArrayList<>();
    29 30       // 打乱牌
    31       Collections.shuffle(poker);
    32 33       for (int i = 0,len = poker.size(); i < len;i++) {
    34         if(i>=51) {
    35           diPai.add(poker.get(i));
    36         }else if(i%3==0) {
    37           list1.add(poker.get(i));
    38         }else if(i%3==1) {
    39           list2.add(poker.get(i));
    40         }else if(i%3==2) {
    41           list3.add(poker.get(i));
    42         }
    43       }
    44 45       System.out.println("周星驰:" + list1);
    46       System.out.println("刘德华:" + list2);
    47       System.out.println("周润发:" + list3);
    48       System.out.println("底牌:" + diPai);
    49 50     }
    51   }

猜你喜欢

转载自www.cnblogs.com/bruce1993/p/11801298.html