zoukankan      html  css  js  c++  java
  • 大数据之路week02 List集合的子类

    1:List集合的子类(掌握)

      (1)List的子类特点

        ArrayList:

          底层数据结构是数组,查询快,增删慢。

          线程不安全,效率高。

        Vector:

          底层数据结构是数组,查询快,增删慢。

          线程安全,效率低。

        LinkedList:

          底层数据结构是链表,查询慢,增删快。

          线程不安全,效率高。

      (2)ArrayList

        A: 没有特有的功能需要学习

        B: 案例:

          a: ArrayList 存储字符串对象并遍历

     1 package com.wyh.jdkNews;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月16日 上午9:16:23
     9 * 
    10 * ArrayList存储字符串并进行遍历,要求加入泛型,并用增强for遍历
    11 * 
    12 * 1、迭代器
    13 * 2、普通for循环
    14 * 3、增强for循环
    15 */
    16 public class ArrayListDemo01 {
    17     public static void main(String[] args) {
    18         ArrayList<String> array = new ArrayList<String>();
    19         
    20         array.add("赵以浩");
    21         array.add("王友虎");
    22         array.add("齐博源");
    23         
    24         //迭代器
    25         Iterator<String> it = array.iterator();
    26         while(it.hasNext()) {
    27             String s = it.next();
    28             System.out.println(s);
    29         }
    30         
    31         System.out.println("----------------");
    32         
    33         
    34         //普通for循环
    35         for(int x = 0;x<array.size();x++) {
    36             String s = array.get(x);
    37             System.out.println(s);
    38         }
    39         
    40         System.out.println("----------------");
    41         
    42         //增强for进行遍历
    43         for(String s : array) {
    44             System.out.println(s);
    45         }
    46     }
    47 }

          b: ArrayList存储自定义对象并遍历

    学生 Student类:

     1 package com.wyh.jdkNews;
     2 
     3 /** 
     4 * @author WYH
     5 * @version 2019年11月14日 下午7:15:01
     6 */
     7 public class Student {
     8     private String name;
     9     private int age;
    10     public Student(String name, int age) {
    11         super();
    12         this.name = name;
    13         this.age = age;
    14     }
    15     public Student() {
    16         super();
    17         // TODO Auto-generated constructor stub
    18     }
    19     public String getName() {
    20         return name;
    21     }
    22     public void setName(String name) {
    23         this.name = name;
    24     }
    25     public int getAge() {
    26         return age;
    27     }
    28     public void setAge(int age) {
    29         this.age = age;
    30     }
    31     
    32     @Override
    33     public boolean equals(Object obj) {
    34         if (this == obj)
    35             return true;
    36         if (obj == null)
    37             return false;
    38         if (getClass() != obj.getClass())
    39             return false;
    40         Student other = (Student) obj;
    41         if (age != other.age)
    42             return false;
    43         if (name == null) {
    44             if (other.name != null)
    45                 return false;
    46         } else if (!name.equals(other.name))
    47             return false;
    48         return true;
    49     }
    50     
    51     
    52 
    53 }

    测试类:

     1 package com.wyh.jdkNews;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月16日 上午9:16:23
     9 * 
    10 * ArrayList存储自定义对象并进行遍历,要求加入泛型,并用增强for遍历
    11 * 
    12 * 1、迭代器
    13 * 2、普通for循环
    14 * 3、增强for循环
    15 */
    16 public class ArrayListDemo02 {
    17     public static void main(String[] args) {
    18         ArrayList<Student> array = new ArrayList<Student>();
    19         
    20         //创建学生对象
    21         Student s1 = new Student("小花",18);
    22         Student s2 = new Student("小月",19);
    23         Student s3 = new Student("小王",22);
    24         Student s4 = new Student("小强",24);
    25         Student s5 = new Student("小紫",16);
    26         
    27         //将学生对象添加到集合中去
    28         array.add(s1);
    29         array.add(s2);
    30         array.add(s3);
    31         array.add(s4);
    32         array.add(s5);
    33         
    34         
    35         //迭代器、
    36         Iterator<Student> it = array.iterator();
    37         
    38         while(it.hasNext()) {
    39             Student s = it.next();
    40             System.out.println(s.getName()+"---"+s.getAge());
    41         }
    42         
    43         System.out.println("---------------");
    44         
    45         //普通for循环
    46         for(int i = 0;i<array.size();i++) {
    47             Student s = array.get(i);
    48             System.out.println(s.getName()+"---"+s.getAge());
    49         }
    50         
    51         System.out.println("----------------");
    52         
    53         //增强for循环
    54         for(Student s : array) {
    55             System.out.println(s.getName()+"---"+s.getAge());
    56             
    57         }
    58         
    59         
    60         
    61     }
    62 }

      (3)Vector

        A: 特有功能

          a: 添加

            public void addElement(E obj)   将指定的组件添加到此向量的末尾,将其大小增加1。 ------add()

          b:获取

            public E elementAt(int index)   返回指定索引处的组件。               -----get()

            public Enumberation<E> elements()     -----Iterator()

      (4)LinkedList

        A: 特有的功能

          a: 添加

            addFirst()

            addLast()

          b: 删除

            removeFirst()

            removeLast()

          c: 获取

            getFirst()

            getLast()

        B: 案例:

          a: LinkedList 存储字符串并且遍历

     1 package com.wyh.LinkedList;
     2 
     3 import java.util.Iterator;
     4 import java.util.LinkedList;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月16日 下午9:40:01
     9 */
    10 public class LinkedListDemo2 {
    11     public static void main(String[] args) {
    12         //创建集合对象
    13         LinkedList linkList = new LinkedList();
    14         
    15         //将学生对象添加到集合中去
    16         linkList.add("aa");
    17         linkList.add("bb");
    18         linkList.add("cc");
    19         linkList.add("dd");
    20         linkList.add("ee");
    21         
    22         //创建迭代器对象
    23         Iterator it = linkList.iterator();
    24         
    25         while(it.hasNext()) {
    26             String s = (String)it.next();
    27             System.out.println(s);
    28         }
    29         
    30         
    31         
    32         
    33     }
    34 }

          b: LinkedList存储自定义对象并遍历

     1 package com.wyh.LinkedList;
     2 
     3 import java.util.Iterator;
     4 import java.util.LinkedList;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月14日 下午7:14:33
     9 * 
    10 * LinkedList 存储对象并遍历。
    11 */
    12 public class LinkedListDemo1 {
    13     public static void main(String[] args) {
    14         //创建集合对象
    15         LinkedList linkList = new LinkedList();
    16         
    17         //创建学生对象
    18         Student s1 = new Student("王友虎",22);
    19         Student s2 = new Student("赵以浩",23);
    20         Student s3 = new Student("李宏灿",21);
    21         Student s4 = new Student("李先锋",22);
    22         Student s5 = new Student("齐博源",23);
    23         
    24         //将学生对象添加到集合中去
    25         linkList.add(s1);
    26         linkList.add(s2);
    27         linkList.add(s3);
    28         linkList.add(s4);
    29         linkList.add(s5);
    30         
    31         //创建迭代器对象
    32         Iterator it = linkList.iterator();
    33         
    34         while(it.hasNext()) {
    35             Student s = (Student)it.next();
    36             System.out.println(s.getName()+"------"+s.getAge());
    37         }
    38         
    39         
    40         
    41         
    42     }
    43 
    44 }

      (5)案例;

        A: 去除集合中的多个字符串的重复元素

          如果字符串的内容相同,即为重复元素。

    a: 思路1:创建一个新的集合,依次遍历旧集合,如果新集合里没有该元素,就添加,反之,有就不添加。  

     1 package com.wyh.LinkedList;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月14日 下午8:53:32
     9 * 
    10 * 去除ArrayList中的重复字符串对象
    11 *   思路1:创建一个新的集合,依次遍历旧集合,如果新集合里没有该元素,就添加,反之,有就不添加。
    12 */
    13 public class ArrayListDemo4 {
    14     public static void main(String[] args) {
    15         //创建集合对象
    16         ArrayList a = new ArrayList();
    17         
    18         a.add("AAA");
    19         a.add("BBB");
    20         a.add("CCC");
    21         a.add("AAA");
    22         a.add("AAA");
    23         a.add("AAA");
    24         a.add("BBB");
    25         a.add("CCC");
    26         
    27         
    28         //创建迭代器
    29         Iterator it = a.iterator();
    30         
    31         //创建一个新的集合
    32         ArrayList newa = new ArrayList();
    33         
    34         
    35         while(it.hasNext()) {
    36             String s = (String) it.next();
    37             if(!(newa.contains(s))) {
    38                 newa.add(s);
    39             }  
    40         }
    41         
    42         //遍历
    43         Iterator it1 = newa.iterator();
    44         while(it1.hasNext()) {
    45             String s = (String)it1.next();
    46             System.out.println(s);
    47         }
    48         
    49     }
    50 
    51 }

    b:  不创建新的集合,就在以前的集合上做。选择排序的思想

     1 package com.wyh.LinkedList;
     2 
     3 import java.util.ArrayList;
     4 
     5 /** 
     6 * @author WYH
     7 * @version 2019年11月14日 下午8:53:39
     8 * 
     9 * 去除ArrayList中的重的字符串对象
    10 *   思路2:不创建新的集合,就在以前的集合上做。选择排序的思想
    11 */
    12 public class ArrayListDemo5 {
    13     public static void main(String[] args) {
    14       //创建集合对象
    15         ArrayList a = new ArrayList();
    16         
    17         a.add("AAA");
    18         a.add("BBB");
    19         a.add("CCC");
    20         a.add("AAA");
    21         a.add("AAA");
    22         a.add("AAA");
    23         a.add("BBB");
    24         a.add("CCC");
    25         
    26         
    27         for(int i = 0;i<a.size()-1;i++) {
    28             for(int j = i+1;j<a.size();j++) {
    29                 if(a.get(i).equals(a.get(j))) {
    30                     a.remove(j);
    31                     j--;
    32                 }
    33             }
    34         }
    35         
    36         
    37         
    38         //遍历
    39         for(int i = 0;i<a.size();i++) {
    40             String s = (String)a.get(i);
    41             System.out.println(s);
    42         }
    43     }
    44 
    45 }

        B: 去除集合中的多个自定义对象的重复元素

          如果自定义对象的成员变量值都相同,即为重复元素。

    知识扩展:

    contains方法的底层是equals方法,所以不能用这个进行比较。
    * 而我们学生类里面没有equals方法,所以这时候就默认使用的是它父亲(Object)的equals方法。
    * 而Object的equals方法,默认比较的是地址值,又因为,new的地址值都不同,所以用equals比较总是失败。
    * 怎么解决?
    * 重写equals方法

    重写后的Student类;

     1 package com.wyh.LinkedList;
     2 
     3 /** 
     4 * @author WYH
     5 * @version 2019年11月14日 下午7:15:01
     6 */
     7 public class Student {
     8     private String name;
     9     private int age;
    10     public Student(String name, int age) {
    11         super();
    12         this.name = name;
    13         this.age = age;
    14     }
    15     public Student() {
    16         super();
    17         // TODO Auto-generated constructor stub
    18     }
    19     public String getName() {
    20         return name;
    21     }
    22     public void setName(String name) {
    23         this.name = name;
    24     }
    25     public int getAge() {
    26         return age;
    27     }
    28     public void setAge(int age) {
    29         this.age = age;
    30     }
    31     
    32     @Override
    33     public boolean equals(Object obj) {
    34         if (this == obj)
    35             return true;
    36         if (obj == null)
    37             return false;
    38         if (getClass() != obj.getClass())
    39             return false;
    40         Student other = (Student) obj;
    41         if (age != other.age)
    42             return false;
    43         if (name == null) {
    44             if (other.name != null)
    45                 return false;
    46         } else if (!name.equals(other.name))
    47             return false;
    48         return true;
    49     }
    50     
    51     
    52 
    53 }

    思路1:创建一个新的集合,依次遍历旧集合,如果新集合里没有该元素,就添加,反之,有就不添加。

     1 package com.wyh.LinkedList;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月14日 下午7:28:06
     9 * 
    10 * 去除ArrayList中的重复对象
    11 *   思路1:创建一个新的集合,依次遍历旧集合,如果新集合里没有该元素,就添加,反之,有就不添加。
    12 *   
    13 *   contains方法的底层是equals方法,所以不能用这个进行比较。
    14 *   而我们学生类里面没有equals方法,所以这时候就默认使用的是它父亲(Object)的equals方法。
    15 *   而Object的equals方法,默认比较的是地址值,又因为,new的地址值都不同,所以用equals比较总是失败。
    16 * 怎么解决?
    17 *   重写equals方法
    18 *   
    19 */
    20 public class ArrayListDemo2 {
    21     public static void main(String[] args) {
    22       //创建集合对象
    23         ArrayList aList = new ArrayList();
    24         
    25         //创建学生对象
    26         Student s1 = new Student("齐博源",22);
    27         Student s2 = new Student("王友虎",22);
    28         Student s3 = new Student("赵以浩",24);
    29         Student s4 = new Student("李先锋",24);
    30         Student s5 = new Student("李宏灿",22);
    31         Student s6 = new Student("李宏灿",22);
    32         Student s7 = new Student("赵以浩",24);
    33         Student s8 = new Student("齐博源",22);
    34         
    35         
    36         
    37         //将学生对象添加到集合中去
    38         aList.add(s1);
    39         aList.add(s2);
    40         aList.add(s3);
    41         aList.add(s4);
    42         aList.add(s5);
    43         aList.add(s6);
    44         aList.add(s7);
    45         aList.add(s8);
    46         
    47         
    48         
    49         
    50         //创建一个新的集合
    51         ArrayList aList2 = new ArrayList();
    52         
    53         
    54         //使用迭代器进行遍历
    55         Iterator it = aList.iterator();
    56         
    57         while(it.hasNext()) {
    58             Student s = (Student)it.next();
    59             System.out.println(s.getName()+"---"+s.getAge());
    60             if(!aList2.contains(s)) {
    61                 aList2.add(s);
    62                 
    63             }
    64         }
    65         
    66         System.out.println("----------去重后-----------");
    67         
    68         //遍历新集合
    69         Iterator it2 = aList2.iterator();
    70         while(it2.hasNext()) {
    71             Student s = (Student)it2.next();
    72             System.out.println(s.getName()+"---"+s.getAge());
    73         }
    74        
    75         
    76     }
    77 
    78 }

    思路2:不创建新的集合,就在以前的集合上做。选择排序的思想

     1 package com.wyh.LinkedList;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月14日 下午8:09:35
     9 * 
    10 * 
    11 * 去除ArrayList中的重复对象
    12 *   思路2:不创建新的集合,就在以前的集合上做。选择排序的思想
    13 */
    14 public class ArrayListDemo3 {
    15     public static void main(String[] args) {
    16       //创建集合对象
    17         ArrayList aList = new ArrayList();
    18         
    19         //创建学生对象
    20         Student s1 = new Student("齐博源",22);
    21         Student s2 = new Student("王友虎",22);
    22         Student s3 = new Student("赵以浩",24);
    23         Student s4 = new Student("李先锋",24);
    24         Student s5 = new Student("李宏灿",22);
    25         Student s6 = new Student("赵以浩",24);
    26         Student s7 = new Student("王友虎",22);
    27         Student s8 = new Student("齐博源",22);
    28         
    29         
    30         
    31         //将学生对象添加到集合中去
    32         aList.add(s1);
    33         aList.add(s2);
    34         aList.add(s3);
    35         aList.add(s4);
    36         aList.add(s5);
    37         aList.add(s6);
    38         aList.add(s7);
    39         aList.add(s8);
    40         
    41         
    42         
    43         for(int i = 0 ;i<aList.size()-1;i++) {
    44             for(int j = i+1;j<aList.size();j++) {
    45                 if(aList.get(i).equals(aList.get(j))) {
    46                     aList.remove(j);// 移除后一个会把前面顶掉,移除第六个,j=5,就不再小于size了
    47                     j--;
    48                 }
    49             }
    50         }
    51         
    52         for(int i = 0;i<aList.size();i++) {
    53             Student s = (Student)aList.get(i);
    54             System.out.println(s.getName()+"---"+s.getAge());
    55         }
    56     }
    57 
    58 }

        C: 用LInkedlIst模拟一个栈数据结构的集合类(面试题!!)

    创建我们模拟LinkedList的类:

     1 package com.wyh.LinkedList;
     2 
     3 import java.util.Iterator;
     4 import java.util.LinkedList;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月14日 下午9:28:24
     9 * 
    10 * 利用LinkedList模拟栈数据结构的集合测试
    11 * 
    12 */
    13 public class MyStack01 {
    14     
    15     private LinkedList list;
    16     
    17     public MyStack01() {
    18         this.list = new LinkedList();
    19     }
    20     
    21     
    22     //添加功能
    23     public void add(Object obj) {
    24         list.addFirst(obj);
    25     }
    26     
    27     //获取功能
    28     public Object get() {
    29 //        return list.getFirst();
    30         return list.removeFirst(); //删除栈顶元素,并且返回被删除的元素.
    31     }
    32 
    33 
    34     public Iterator iterator() {
    35         // TODO Auto-generated method stub
    36         return list.iterator();
    37     }
    38 
    39 
    40     public int size() {
    41         // TODO Auto-generated method stub
    42         return list.size();
    43     }
    44     
    45     public boolean isEmpty() {
    46         return list.isEmpty();
    47     }
    48 
    49 }

    创建测试类:

     1 package com.wyh.LinkedList;
     2 
     3 import java.util.Iterator;
     4 
     5 /** 
     6 * @author WYH
     7 * @version 2019年11月14日 下午9:36:12
     8 */
     9 public class MyStackTest01 {
    10     public static void main(String[] args) {
    11         MyStack01 ms = new MyStack01();
    12         
    13         ms.add("123");
    14         ms.add("321");
    15         ms.add("789");
    16         
    17         /*for(int i = ms.size();i>0;i--) {
    18             System.out.println(ms.get());
    19         }*/
    20         
    21         
    22         //while循环
    23         while(!ms.isEmpty()) {
    24             System.out.println(ms.get());
    25         }
    26         
    27         
    28         
    29         /*//for循环
    30         for(int i = 0;i<=ms.size()+1;i++) {
    31             System.out.println(ms.get());
    32         }*/
    33         
    34         
    35        /* //创建迭代器
    36         Iterator it = ms.iterator();
    37         
    38         while(it.hasNext()) {
    39             String s = (String)it.next();
    40             System.out.println(s);
    41         }*/
    42         
    43     }
    44 
    45 }

    2、泛型(掌握)

      (1)泛型概述  

        是一种把明确类型的工作推迟到创建对象或者调用该方法的时候才去明确的特殊类型

      (2)格式

        <数据类型>

        注意: 该数据类型只能是引用类型

      (3)好处:

        A: 把运行时期出现的问题提前到了编译时期

        B: 避免了强制类型转换

        C: 优化了程序设计,解决了黄色警告线的问题,让程序更加的安全。

      (4)泛型的前生今世

        A: 泛型的由来 

          早期的时候,我们使用Object来表示任意类型。
          向下转型的时候是没有任何问题的,但是在向下转型的时候隐含了转型的问题。
          也就是说这样的程序时不安全的,所以Java在JDK1.5的之后,引入了泛型,提高系统的安全性。

          Object类型作为任意类型的时候,在向下转型的时候,会隐含一个转型问题。

        B: 泛型类

    泛型类:

     1 package com.wyh.Object;
     2 
     3 /** 
     4 * @author WYH
     5 * @version 2019年11月15日 下午9:09:39
     6 * 
     7 * 把泛型应用在类上,泛型类
     8 */
     9 public class ObjectTool02<T> {
    10     private T obj;
    11 
    12     public T getObj() {
    13         return obj;
    14     }
    15 
    16     public void setObj(T obj) {
    17         this.obj = obj;
    18     }
    19     
    20 }

    泛型类的测试:

     1 package com.wyh.Object;
     2 
     3 /** 
     4 * @author WYH
     5 * @version 2019年11月15日 下午9:11:09
     6 * 
     7 * 泛型类测试
     8 */
     9 public class ObjectToolDemo02 {
    10     public static void main(String[] args) {
    11         ObjectTool02<String> obj = new ObjectTool02<String>();
    12         //obj.setObj(new Integer(2));//编译的时候就会报错
    13         obj.setObj(new String("小虎"));
    14         String i = obj.getObj();
    15         System.out.println(i);
    16         
    17         ObjectTool02<Integer> obj2 = new ObjectTool02<Integer>();
    18         //obj2.setObj(new String("xiaohu")); //编译的时候就会报错
    19         obj2.setObj(new Integer(23));
    20         Integer j = obj2.getObj();
    21         System.out.println(j);
    22         
    23     }
    24 }

        C: 泛型方法

    泛型方法,类上不加泛型,在方法中添加:

     1 package com.wyh.Object;
     2 
     3 /** 
     4 * @author WYH
     5 * @version 2019年11月15日 下午9:26:17
     6 * 
     7 * 泛型方法
     8 * 
     9 */
    10 
    11 
    12 //泛型方法
    13 public class ObjectTool03 {
    14     public <T> void show(T t) {
    15         System.out.println(t);
    16     }
    17 }

    测试:

     1 package com.wyh.Object;
     2 
     3 /** 
     4 * @author WYH
     5 * @version 2019年11月15日 下午9:28:22
     6 */
     7 public class ObjectToolDemo03 {
     8     public static void main(String[] args) {
     9         //问题1
    10         /*ObjectTool03 obj = new ObjectTool03();
    11         obj.show("王友虎");
    12         obj.show(123);
    13         obj.show(true);*/
    14         
    15       //问题2
    16         /*ObjectTool03<String> obj = new ObjectTool03<String>();
    17         obj.show("王友虎");
    18         //obj.show(123);
    19         //obj.show(true);
    20          */
    21         
    22         //问题3、如果到这还听得懂,说明泛型类是没有问题的
    23         //但是,我现在定义的类和方法的类型是一致的,谁说过一定要一致的呢?
    24         //如果类上没有定义泛型的话,那么还能不能接收任意类型的呢?(答案是肯定可以的,泛型方法)ObjectTool03 第二段代码
    25         ObjectTool03 obj = new ObjectTool03();
    26         obj.show("王友虎");
    27         obj.show(123);
    28         obj.show(true);
    29         
    30         
    31     }
    32 
    33 }

        D: 泛型接口

    创建泛型接口:

     1 package com.wyh.Object;
     2 
     3 /** 
     4 * @author WYH
     5 * @version 2019年11月15日 下午9:46:16
     6 * 
     7 * 泛型接口
     8 */
     9 public interface Inter<T> {
    10     public abstract void show(T t);
    11 }

    创建实现类将接口实现:

     1 package com.wyh.Object;
     2 
     3 /** 
     4 * @author WYH
     5 * @version 2019年11月15日 下午9:47:30
     6 */
     7 
     8 
     9 //第一种情况,我们在实现的时候就知道是什么类型的了。
    10 /*public class InterImpl implements Inter<String>{
    11 
    12     @Override
    13     public void show(String s) {
    14         System.out.println(s);
    15         
    16     }
    17 }*/
    18 
    19 //第二种情况,
    20 public class InterImpl<T> implements Inter<T>{
    21 
    22     @Override
    23     public void show(T t) {
    24         System.out.println(t);
    25         
    26     }
    27 }

    创建测试类进行测试:

     1 package com.wyh.Object;
     2 
     3 /** 
     4 * @author WYH
     5 * @version 2019年11月15日 下午9:48:09
     6 */
     7 public class InterTest {
     8     public static void main(String[] args) {
     9         
    10         /*//第一种情况测试
    11         Inter<String> inte = new InterImpl();
    12         inte.show("王友虎");*/
    13         
    14       //第一种情况测试
    15         Inter<String> inte = new InterImpl<String>();
    16         inte.show("王友虎");
    17         
    18         Inter<Integer> inte1 = new InterImpl<Integer>();
    19         inte1.show(123);
    20         
    21     
    22     }
    23 
    24 }

        E: 泛型高级之通配符

          ?: 任意类型,如果没有明确,那就是Object类型以及任意的Java类了
          ? extends E: 向下限定,限定E 及其子类
          ? super E: 向上限定,限定E 及其父类

     1 package com.wyh.generic;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collection;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月15日 下午10:00:39
     9 * 
    10 * 泛型之高级通配符
    11 * 
    12 * ?: 任意类型,如果没有明确,那就是Object类型以及任意的Java类了
    13 * ? extends E: 向下限定,限定E 及其子类
    14 * ? super E:  向上限定,限定E 及其父类
    15 */
    16 
    17 class Animal{
    18     
    19 }
    20 
    21 class Dog extends Animal{
    22     
    23 }
    24 
    25 class Cat extends Animal{
    26     
    27 }
    28 
    29 public class GenericDemo1 {
    30     public static void main(String[] args) {
    31 
    32         // 泛型如果明确写的时候,前后必须一致!
    33         Collection<Object> c1 = new ArrayList<Object>();
    34         /*
    35          * Collection<Object> c2 = new ArrayList<Animal>(); Collection<Object> c3 = new
    36          * ArrayList<Dog>(); Collection<Object> c4 = new ArrayList<Cat>();
    37          */
    38 
    39         // ? 表示任一类型是可以滴
    40         Collection<?> c5 = new ArrayList<Object>();
    41         Collection<?> c2 = new ArrayList<Animal>();
    42         Collection<?> c3 = new ArrayList<Dog>();
    43         Collection<?> c4 = new ArrayList<Cat>();
    44 
    45         // ? extends E: 向下限定,限定E 及其子类
    46         // Collection<? extends Animal> c6 = new ArrayList<Object>(); //报错
    47         Collection<? extends Animal> c7 = new ArrayList<Dog>();
    48         Collection<? extends Animal> c8 = new ArrayList<Cat>();
    49         Collection<? extends Animal> c9 = new ArrayList<Animal>();
    50 
    51         // ? super E: 向上限定,限定E 及其父类
    52         Collection<? super Animal> c10 = new ArrayList<Object>();
    53         Collection<? super Animal> c13 = new ArrayList<Animal>();
    54 //        Collection<? super Animal> c11 = new ArrayList<Dog>(); //报错
    55 //        Collection<? super Animal> c12 = new ArrayList<Cat>();
    56         
    57         
    58         
    59     }
    60 }

    3、增强for循环(掌握)增强for其实就是来替代器迭代器的,怎么验证? 并发修改异常

      (1)是for循环的一种

      (2)格式:

        for(元素的数据类型 变量名 :数组或者Collection集合的对象){

          使用该变量即可,该变量其实就是数组或者集合中的元素。

        }

      (3)好处:

        简化了数组和集合的遍历

      (4)弊端

        增强for循环的目标不能为null。建议在使用前,现判断是否为null。

     1 package com.wyh.jdkNews;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月16日 上午8:44:59
     9 * 
    10 * JDK5 的新特性: 自动装箱,泛型,增强for,静态导入,可变参数,枚举
    11 * 
    12 * 增强for: for循环
    13 *  格式: 
    14 *       for(元素数据类型 变量名 : 数组或者是Collection 集合){
    15 *           变量,直接使用
    16 *       }
    17 *       
    18 * 好处: 简化了数组和集合的遍历
    19 * 
    20 * 弊端: 增强for的目标不能为null
    21 * 解决: 在进行增强for之前我们要先进行不为null的判断 
    22 */
    23 public class ForDemo01 {
    24     public static void main(String[] args) {
    25         int[] arr = {1,2,3,4,5};
    26         
    27         for(int x = 0;x<arr.length;x++) {
    28             System.out.println(arr[x]);
    29         }
    30         
    31         System.out.println("---------------");
    32         
    33         for(int x:arr) {
    34             System.out.println(x);
    35         }
    36         
    37         System.out.println("---------------");
    38         
    39         //定义一个字符串数组
    40         String[] arrayList = {"小虎","小沛","小王"};
    41         for(String array : arrayList) {
    42             System.out.println(array);
    43             
    44         }
    45         
    46         System.out.println("---------------");
    47         // 定义一个ArrayList
    48         ArrayList<String> array = new ArrayList<String>();
    49         array.add("林青霞");
    50         array.add("刘亦菲");
    51         array.add("景甜");
    52 
    53         for (String array1 : array) {
    54             System.out.println(array1);
    55 
    56         }
    57         
    58         System.out.println("---------------");
    59         
    60         //NullPointerException报空指针错误
    61         //
    62         
    63         List<String> list = null;
    64         if (list != null) {
    65             for (String x : list) {
    66                 System.out.println(x);
    67             }
    68         }
    69        
    70         System.out.println("---------------");
    71         //增强for其实就是来替代器迭代器的,怎么验证? 并发修改异常。ConcurrentModificationException
    72         for (String array1 : array) {
    73             if("刘亦菲".equals(array1)) {
    74                 array.add("小王");
    75             }
    76 
    77         }
    78         System.out.println("array1:"+array);
    79         
    80         
    81     }
    82     
    83     
    84 
    85 }

    4、静态导入(了解)

      (1)可以导入到方法级别的导入

      (2)格式

        import static 包名....类名.方法名;

      (3)注意事项:

        A:  方法必须是静态的

        B:如果多个类下有同名的方法,就不好区分了。还得加上前缀,所以一般我们并不使用静态导入,但是一定要能够看得懂。

     1 package com.wyh.StaticImport;
     2 
     3 /** 
     4 * @author WYH
     5 * @version 2019年11月16日 上午9:40:42
     6 * 静态导入
     7 * 
     8 * 格式: import static 包名....类名.方法名
     9 * 可以直接导入到方法级别
    10 */
    11 import static java.lang.Math.abs;
    12 import static java.lang.Math.pow;
    13 import static java.lang.Math.max;
    14 
    15 
    16 public class StaticImportDemo01 {
    17     public static void main(String[] args) {
    18         
    19         System.out.println(java.lang.Math.abs(100));
    20         System.out.println(java.lang.Math.pow(2, 4));
    21         System.out.println(java.lang.Math.max(10, 50));
    22         //太复杂
    23         
    24         System.out.println(Math.abs(100));
    25         System.out.println(Math.pow(2, 4));
    26         System.out.println(Math.max(10, 50));
    27         //依旧太复杂
    28         
    29         System.out.println(abs(100));
    30         System.out.println(pow(2, 4));
    31         System.out.println(java.lang.Math.max(10, 50));
    32         //静态导包
    33     }
    34     
    35     private static void max(int i) {//底层源码就是数组
    36         System.out.println(i);
    37     }
    38 }

    5、可变参数(掌握)

      (1)如果我们在写方法的时候,参数个数不明确,就应该定义可变参数。

      (2)格式:

        修饰符 返回值类型  方法名(数据类型... 变量){ }

        注意:

          A: 该变量其实是一个数组名

          B: 如果一个方法有多个参数,并且有可变参数,可变参数必须在最后。

      (3)Arrays工具类的一个方法

        asList()把数组转成集合

        注意: 这个集合的长度不能改变。

     1 package com.wyh.args;
     2 
     3 /** 
     4 * @author WYH
     5 * @version 2019年11月16日 上午9:52:41
     6 * 
     7 * 可变参数:
     8 *   访问修饰符 返回值  方法名(数据类型... 变量名){
     9 *       
    10 *   }
    11 *   
    12 * 1.这里的变量名是一个数组
    13 * 2.如果一个方法拥有可变参数,并且拥有多个参数,那么可变参数是放在最后一个。
    14 *   
    15 * 
    16 * 需求:我要写一个求和函数,具体几个数据求和我并不清楚,但是我在调用的时候就知道了.
    17 * 为了解决这个问题,Java就提供了一个东西: 可变参数
    18 */
    19 public class ArgsDemo01 {
    20     public static void main(String[] args) {
    21         //两个数据的求和
    22         int a = 10;
    23         int b = 20;
    24         int result = sum(a, b);
    25         System.out.println("result:"+result);
    26         
    27       //三个数据的求和
    28         int c = 30;
    29         int result2 = sum(a,b,c);
    30         System.out.println("result:"+result2);
    31         
    32       //四个数据的求和
    33         int d = 40;
    34         int result3 = sum(a,b,c,d);
    35         System.out.println("result:"+result3);
    36         
    37         
    38     }
    39     
    40     //可变参数
    41     private static int sum(int... a) {//底层源码就是数组
    42         int s = 0;
    43         for(int x : a) {
    44             s += x;
    45         }
    46         return s;
    47     }
    48     
    49    /* private static int sum(int a, int b,int c,int d) {
    50         return a+b+c+d;
    51     }
    52 
    53     private static int sum(int a, int b,int c) {
    54         return a+b+c;
    55     }
    56     
    57     
    58     private static int sum(int a, int b) {
    59         return a+b;
    60     }*/
    61 
    62 }
     1 package com.wyh.args;
     2 
     3 import java.util.Arrays;
     4 import java.util.List;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月16日 上午10:15:37
     9 * 
    10 * static <T> List<T> asList(T... a) :把数组转成一个集合
    11 * 注意:
    12 *   虽然是把一个数组转成一个集合,但是其本质是数组,所以,它的长度固定
    13 */
    14 public class ArraysDemo01 {
    15     public static void main(String[] args) {
    16         String[] s = {"小虎","小花","小美"};
    17         
    18         List<String> list = Arrays.asList(s);
    19 //        UnsupportedOperationException
    20 //        list.add("123");
    21 //        UnsupportedOperationException
    22 //        list.remove(1);
    23         list.set(1,"小雪");  //可以进行修改
    24         
    25         
    26         for(String s1 : list) {
    27             System.out.println(s1);
    28         }
    29     }
    30 
    31 }

    6、练习(掌握)

      A: 集合的嵌套遍历。

    先来看看逻辑图:

     代码实现:(学生类不重复给出了)

     1 package com.wyh.list_son_test;
     2 
     3 import java.util.ArrayList;
     4 
     5 /** 
     6 * @author WYH
     7 * @version 2019年11月16日 上午10:28:03
     8 * 
     9 * 集合的嵌套遍历
    10 * 
    11 * 需求:
    12 *   我们班里有学生,每一个学生可以看作是一个对象,可以使用一个集合来表示我们一个班的学生 ,ArrayList<Student>
    13 *   但是呢,隔壁也有一个班级,也同样拥有一个ArrayList<Student>来表示一个班的学生
    14 *   而我们现在有多个ArrayList<Student>,也是要用集合来进行存储,怎么办呢?
    15 *   其实就是 ArrayList<ArrayList<Student>>
    16 */
    17 public class ArrayListDemo01 {
    18     public static void main(String[] args) {
    19         //创建大集合对象
    20         ArrayList<ArrayList<Student>> bigArrayList = new ArrayList<ArrayList<Student>>();
    21         
    22         //创建第一个班级
    23         ArrayList<Student> firstArrayList = new ArrayList<Student>();
    24         //创建学生对象
    25         Student s1 = new Student("唐三藏",30);
    26         Student s2 = new Student("孙悟空",29);
    27         Student s3 = new Student("猪八戒",28);
    28         Student s4 = new Student("白龙马",27);
    29         Student s5 = new Student("沙悟净",26);
    30         //将学生对象添加到集合中去
    31         firstArrayList.add(s1);
    32         firstArrayList.add(s2);
    33         firstArrayList.add(s3);
    34         firstArrayList.add(s4);
    35         firstArrayList.add(s5);
    36         //再将这个班级放到学生系统中去
    37         bigArrayList.add(firstArrayList);
    38         
    39         
    40       //创建第二个班级
    41         ArrayList<Student> secondArrayList = new ArrayList<Student>();
    42         //创建学生对象
    43         Student s11 = new Student("刘备",40);
    44         Student s22 = new Student("关羽",38);
    45         Student s33 = new Student("张飞",35);
    46         //将学生对象添加到集合中去
    47         secondArrayList.add(s11);
    48         secondArrayList.add(s22);
    49         secondArrayList.add(s33);
    50         //再将这个班级放到学生系统中去
    51         bigArrayList.add(secondArrayList);
    52         
    53         
    54       //创建第三个班级
    55         ArrayList<Student> thirdArrayList = new ArrayList<Student>();
    56         //创建学生对象
    57         Student s111 = new Student("宋江",29);
    58         Student s222 = new Student("鲁智深",45);
    59         Student s333 = new Student("武松",28);
    60         //将学生对象添加到集合中去
    61         thirdArrayList.add(s111);
    62         thirdArrayList.add(s222);
    63         thirdArrayList.add(s333);
    64         //再将这个班级放到学生系统中去
    65         bigArrayList.add(thirdArrayList);
    66         
    67         //增强for循环进行遍历
    68         for(ArrayList<Student> array : bigArrayList) {
    69             System.out.println("---------");
    70             for(Student i : array) {
    71                 System.out.println(i.getName()+"---"+i.getAge());
    72             }
    73         }
    74         
    75         
    76         
    77     }
    78 
    79 }

      B: 产生10个1-20之间的随机数,要求随机数不能重复。

     1 package com.wyh.random;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Random;
     5 
     6 /** 
     7 * @author WYH
     8 * @version 2019年11月16日 上午11:13:17
     9 * 随机产生10个1-20之间的数,不能重复
    10 * 
    11 * 可以用集合实现
    12 * 
    13 * 1、创建产生随机数对象
    14 * 2、创建存储产生随机数的集合
    15 * 3、创建统计随机数个数的变量,从0 开始
    16 * 4、判断统计变量是否小于10,
    17 *               是:不搭理它,跳过
    18 *               不是: 先产生一个随机数,判断在集合中是否存在,
    19 *                       存在: 不搭理他
    20 *                       不存在: 进行添加,统计变量+1
    21 * 
    22 */
    23 public class RandomDemo01 {
    24     public static void main(String[] args) {
    25         //创建产生随机数对象
    26         Random r = new Random();
    27         
    28         //创建存储产生随机数的集合
    29         ArrayList<Integer> array = new ArrayList<Integer>();
    30         
    31         //创建统计随机数个数的变量,从0 开始
    32         int count = 0;
    33         
    34         //判断统计变量是否小于10,
    35         while(count<10) {
    36             int number = r.nextInt(20)+1;  //r.nextInt(20) 范围在0-19 对他进行+1 保证在1-20
    37             if(!array.contains(number)) {
    38                 array.add(number);
    39                 count++;
    40             }
    41         }
    42         
    43         
    44         for(Integer i : array) {
    45             System.out.println(i);
    46         }
    47         
    48     }
    49 
    50 }

      C: 键盘录入多个数据,以0结束,并在控制台输出最大值。

     1 package com.wyh.random;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Arrays;
     5 import java.util.Scanner;
     6 
     7 /** 
     8 * @author WYH
     9 * @version 2019年11月16日 下午3:20:33
    10 */
    11 public class ScannerDemo01 {
    12     public static void main(String[] args) {
    13         //创建控制台输入对象
    14         Scanner sc = new Scanner(System.in);
    15         
    16         //创建存储从控制台输入的数的集合
    17         ArrayList<Integer> array = new ArrayList<Integer>();
    18         
    19         
    20         
    21         boolean flag = true;
    22         
    23         while(flag) {
    24             // 友情提示
    25             System.out.print("请输入所要存储的数据,输入  0 表示停止:");
    26             int number = sc.nextInt();
    27             if(number==0) {
    28                 flag = false;
    29             }else if(!array.contains(number)){
    30                 array.add(number);
    31             }
    32         }
    33         
    34         Object[] obj = array.toArray();
    35         Arrays.sort(obj);
    36         for(Object x : obj) {
    37             System.out.print(x+" ");  
    38         }
    39         System.out.println("您录入的最大值是:"+obj[obj.length-1]);
    40         
    41        /* //遍历集合
    42         System.out.print("您总共存入的数据有:");
    43         for(Integer i : array) {
    44             System.out.print(i+" ");
    45         }*/
    46         
    47         
    48         
    49     }
    50 
    51 }

    7、总之,学完这一节需要掌握的代码有:

      集合存储元素,加入泛型,并且可以使用增强for循环遍历

  • 相关阅读:
    WSGI详解
    WSGI、flup、fastcgi、web.py的关系
    全面解读python web 程序的9种部署方式
    python对web服务器做压力测试并做出图形直观显示
    6个最佳的开源Python应用服务器
    用 Python 脚本实现对 Linux 服务器的监控
    浅谈图片服务器的架构演进
    Python中使用Flask、MongoDB搭建简易图片服务器
    流行python服务器框架
    搜索引擎技术之概要预览
  • 原文地址:https://www.cnblogs.com/wyh-study/p/11874518.html
Copyright © 2011-2022 走看看