zoukankan      html  css  js  c++  java
  • Java笔记(16):集合框架(02)

    1、ArrayList存储字符串并遍历

     1 package cn.itcast_01;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /*
     7  * List的子类特点:
     8  *         ArrayList:
     9  *             底层数据结构是数组,查询快,增删慢
    10  *             线程不安全,效率高
    11  *         Vector:
    12  *             底层数据结构是数组,查询快,增删慢
    13  *             线程安全,效率低
    14  *         LinkedList:
    15  *              底层数据结构是链表,查询慢,增删快
    16  *             线程不安全,效率高
    17  * 
    18  * 案例:
    19  *         使用List的任何子类存储字符串或者存储自定义对象并遍历。
    20  * 
    21  * ArrayList的使用。    
    22  *         存储字符串并遍历
    23  */
    24 public class ArrayListDemo {
    25     public static void main(String[] args) {
    26         // 创建集合对象
    27         ArrayList array = new ArrayList();
    28 
    29         // 创建元素对象,并添加元素
    30         array.add("hello");
    31         array.add("world");
    32         array.add("java");
    33 
    34         // 遍历
    35         Iterator it = array.iterator();
    36         while (it.hasNext()) {
    37             String s = (String) it.next();
    38             System.out.println(s);
    39         }
    40 
    41         System.out.println("-----------");
    42 
    43         for (int x = 0; x < array.size(); x++) {
    44             String s = (String) array.get(x);
    45             System.out.println(s);
    46         }
    47     }
    48 }

    2、ArrayList存储自定义对象并遍历

     1 package cn.itcast_01;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /*
     7  * ArrayList存储自定义对象并遍历
     8  */
     9 public class ArrayListDemo2 {
    10     public static void main(String[] args) {
    11         // 创建集合对象
    12         ArrayList array = new ArrayList();
    13 
    14         // 创建学生对象
    15         Student s1 = new Student("武松", 30);
    16         Student s2 = new Student("鲁智深", 40);
    17         Student s3 = new Student("林冲", 36);
    18         Student s4 = new Student("杨志", 38);
    19 
    20         // 添加元素
    21         array.add(s1);
    22         array.add(s2);
    23         array.add(s3);
    24         array.add(s4);
    25 
    26         // 遍历
    27         Iterator it = array.iterator();
    28         while (it.hasNext()) {
    29             Student s = (Student) it.next();
    30             System.out.println(s.getName() + "---" + s.getAge());
    31         }
    32 
    33         System.out.println("----------------");
    34 
    35         for (int x = 0; x < array.size(); x++) {
    36             // ClassCastException 注意,千万要搞清楚类型
    37             // String s = (String) array.get(x);
    38             // System.out.println(s);
    39 
    40             Student s = (Student) array.get(x);
    41             System.out.println(s.getName() + "---" + s.getAge());
    42         }
    43     }
    44 }

    3、Vector的特有功能

     1 package cn.itcast_02;
     2 
     3 import java.util.Enumeration;
     4 import java.util.Vector;
     5 
     6 /*
     7  * Vector的特有功能:
     8  * 1:添加功能
     9  *         public void addElement(Object obj)        --    add()
    10  * 2:获取功能
    11  *         public Object elementAt(int index)        --  get()
    12  *         public Enumeration elements()            --    Iterator iterator()
    13  *                 boolean hasMoreElements()                hasNext()
    14  *                 Object nextElement()                    next()
    15  * 
    16  * JDK升级的原因:
    17  *         A:安全
    18  *         B:效率
    19  *         C:简化书写
    20  */
    21 public class VectorDemo {
    22     public static void main(String[] args) {
    23         // 创建集合对象
    24         Vector v = new Vector();
    25 
    26         // 添加功能
    27         v.addElement("hello");
    28         v.addElement("world");
    29         v.addElement("java");
    30 
    31         // 遍历
    32         for (int x = 0; x < v.size(); x++) {
    33             String s = (String) v.elementAt(x);
    34             System.out.println(s);
    35         }
    36 
    37         System.out.println("------------------");
    38 
    39         Enumeration en = v.elements(); // 返回的是实现类的对象
    40         while (en.hasMoreElements()) {
    41             String s = (String) en.nextElement();
    42             System.out.println(s);
    43         }
    44     }
    45 }

    4、LinkedList的特有功能

     1 package cn.itcast_03;
     2 
     3 import java.util.LinkedList;
     4 
     5 /*
     6  * LinkedList的特有功能:
     7  *         A:添加功能
     8  *             public void addFirst(Object e)
     9  *             public void addLast(Object e)
    10  *         B:获取功能
    11  *             public Object getFirst()
    12  *             public Obejct getLast()
    13  *         C:删除功能
    14  *             public Object removeFirst()
    15  *             public Object removeLast()
    16  */
    17 public class LinkedListDemo {
    18     public static void main(String[] args) {
    19         // 创建集合对象
    20         LinkedList link = new LinkedList();
    21 
    22         // 添加元素
    23         link.add("hello");
    24         link.add("world");
    25         link.add("java");
    26 
    27         // public void addFirst(Object e)
    28         // link.addFirst("javaee");
    29         // public void addLast(Object e)
    30         // link.addLast("android");
    31 
    32         // public Object getFirst()
    33         // System.out.println("getFirst:" + link.getFirst());
    34         // public Obejct getLast()
    35         // System.out.println("getLast:" + link.getLast());
    36 
    37         // public Object removeFirst()
    38         System.out.println("removeFirst:" + link.removeFirst());
    39         // public Object removeLast()
    40         System.out.println("removeLast:" + link.removeLast());
    41 
    42         // 输出对象名
    43         System.out.println("link:" + link);
    44     }
    45 }

    练习:去除ArrayList集合中的重复字符串元素案例1

     1 package cn.itcast_04;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /*
     7  * ArrayList去除集合中字符串的重复值(字符串的内容相同)
     8  * 
     9  * 分析:
    10  *         A:创建集合对象
    11  *         B:添加多个字符串元素(包含内容相同的)
    12  *         C:创建新集合
    13  *         D:遍历旧集合,获取得到每一个元素
    14  *         E:拿这个元素到新集合去找,看有没有
    15  *             有:不搭理它
    16  *             没有:就添加到新集合
    17  *         F:遍历新集合
    18  */
    19 public class ArrayListDemo {
    20     public static void main(String[] args) {
    21         // 创建集合对象
    22         ArrayList array = new ArrayList();
    23 
    24         // 添加多个字符串元素(包含内容相同的)
    25         array.add("hello");
    26         array.add("world");
    27         array.add("java");
    28         array.add("world");
    29         array.add("java");
    30         array.add("world");32 
    33         // 创建新集合
    34         ArrayList newArray = new ArrayList();
    35 
    36         // 遍历旧集合,获取得到每一个元素
    37         Iterator it = array.iterator();
    38         while (it.hasNext()) {
    39             String s = (String) it.next();
    40 
    41             // 拿这个元素到新集合去找,看有没有
    42             if (!newArray.contains(s)) {
    43                 newArray.add(s);
    44             }
    45         }
    46 
    47         // 遍历新集合
    48         for (int x = 0; x < newArray.size(); x++) {
    49             String s = (String) newArray.get(x);
    50             System.out.println(s);
    51         }
    52     }
    53 }

    练习:去除ArrayList集合中的重复字符串元素案例2

     1 package cn.itcast_04;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /*
     7  * 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)
     8  * 要求:不能创建新的集合,就在以前的集合上做。
     9  */
    10 public class ArrayListDemo2 {
    11     public static void main(String[] args) {
    12         // 创建集合对象
    13         ArrayList array = new ArrayList();
    14 
    15         // 添加多个字符串元素(包含内容相同的)
    16         array.add("hello");
    17         array.add("world");
    18         array.add("java");
    19         array.add("world");
    20         array.add("java");
    21         array.add("world");
    22         array.add("world");
    23         array.add("world");
    24         array.add("world");
    25         array.add("java");
    26         array.add("world");
    27 
    28         // 由选择排序思想引入,我们就可以通过这种思想做这个题目
    29         // 拿0索引的依次和后面的比较,有就把后的干掉
    30         // 同理,拿1索引...
    31         for (int x = 0; x < array.size() - 1; x++) {
    32             for (int y = x + 1; y < array.size(); y++) {
    33                 if (array.get(x).equals(array.get(y))) {
    34                     array.remove(y);
    35                     y--;
    36                 }
    37             }
    38         }
    39 
    40         // 遍历集合
    41         Iterator it = array.iterator();
    42         while (it.hasNext()) {
    43             String s = (String) it.next();
    44             System.out.println(s);
    45         }
    46     }
    47 }

    5、去除ArrayList集合中的重复自定义对象元素案例

     1 package cn.itcast_04;
     2 
     3 public class Student {
     4     private String name;
     5     private int age;
     6 
     7     public Student() {
     8         super();
     9     }
    10 
    11     public Student(String name, int age) {
    12         super();
    13         this.name = name;
    14         this.age = age;
    15     }
    16 
    17     public String getName() {
    18         return name;
    19     }
    20 
    21     public void setName(String name) {
    22         this.name = name;
    23     }
    24 
    25     public int getAge() {
    26         return age;
    27     }
    28 
    29     public void setAge(int age) {
    30         this.age = age;
    31     }
    32 
    33     @Override
    34     public boolean equals(Object obj) {
    35         if (this == obj)
    36             return true;
    37         if (obj == null)
    38             return false;
    39         if (getClass() != obj.getClass())
    40             return false;
    41         Student other = (Student) obj;
    42         if (age != other.age)
    43             return false;
    44         if (name == null) {
    45             if (other.name != null)
    46                 return false;
    47         } else if (!name.equals(other.name))
    48             return false;
    49         return true;
    50     }
    51 
    52 }
     1 package cn.itcast_04;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /*
     7  * 需求:去除集合中自定义对象的重复值(对象的成员变量值都相同)
     8  * 
     9  * 我们按照和字符串一样的操作,发现出问题了。
    10  * 为什么呢?
    11  *         我们必须思考哪里会出问题?
    12  *         通过简单的分析,我们知道问题出现在了判断上。
    13  *         而这个判断功能是集合自己提供的,所以我们如果想很清楚的知道它是如何判断的,就应该去看源码。
    14  * contains()方法的底层依赖的是equals()方法。
    15  * 而我们的学生类中没有equals()方法,这个时候,默认使用的是它父亲Object的equals()方法
    16  * Object()的equals()默认比较的是地址值,所以,它们进去了。因为new的东西,地址值都不同。
    17  * 按照我们自己的需求,比较成员变量的值,重写equals()即可。
    18  * 自动生成即可。
    19  */
    20 public class ArrayListDemo3 {
    21     public static void main(String[] args) {
    22         // 创建集合对象
    23         ArrayList array = new ArrayList();
    24 
    25         // 创建学生对象
    26         Student s1 = new Student("林青霞", 27);
    27         Student s2 = new Student("林志玲", 40);
    28         Student s3 = new Student("凤姐", 35);
    29         Student s4 = new Student("芙蓉姐姐", 18);
    30         Student s5 = new Student("翠花", 16);
    31         Student s6 = new Student("林青霞", 27);
    32         Student s7 = new Student("林青霞", 18);
    33 
    34         // 添加元素
    35         array.add(s1);
    36         array.add(s2);
    37         array.add(s3);
    38         array.add(s4);
    39         array.add(s5);
    40         array.add(s6);
    41         array.add(s7);
    42 
    43         // 创建新集合
    44         ArrayList newArray = new ArrayList();
    45 
    46         // 遍历旧集合,获取得到每一个元素
    47         Iterator it = array.iterator();
    48         while (it.hasNext()) {
    49             Student s = (Student) it.next();
    50 
    51             // 拿这个元素到新集合去找,看有没有
    52             if (!newArray.contains(s)) {
    53                 newArray.add(s);
    54             }
    55         }
    56 
    57         // 遍历新集合
    58         for (int x = 0; x < newArray.size(); x++) {
    59             Student s = (Student) newArray.get(x);
    60             System.out.println(s.getName() + "---" + s.getAge());
    61         }
    62     }
    63 }

    6、用LinkedList实现栈结构的集合代码

     1 package cn.itcast_05;
     2 
     3 import java.util.Iterator;
     4 import java.util.LinkedList;
     5 
     6 /*
     7  *请用LinkedList模拟栈数据结构的集合,并测试
     8  *题目的意思是:
     9  *        你自己的定义一个集合类,在这个集合类内部可以使用LinkedList模拟。
    10  */
    11 public class LinkedListDemo {
    12     public static void main(String[] args) {
    13         // A: LinkedList的特有添加功能addFirst()
    14         // B:栈的特点先进后出
    15         // 创建集合对象
    16         // LinkedList link = new LinkedList();
    17         //
    18         // // 添加元素
    19         // link.addFirst("hello");
    20         // link.addFirst("world");
    21         // link.addFirst("java");
    22         //
    23         // // 遍历
    24         // Iterator it = link.iterator();
    25         // while (it.hasNext()) {
    26         // String s = (String) it.next();
    27         // System.out.println(s);
    28         // }
    29         
    30         //这样做是错误的,为什么呢?
    31     }
    32 }
     1 package cn.itcast_05;
     2 
     3 import java.util.LinkedList;
     4 
     5 /**
     6  * 自定义的栈集合
     7  * 
     8  * @author 风清扬
     9  * @version V1.0
    10  */
    11 public class MyStack {
    12     private LinkedList link;
    13 
    14     public MyStack() {
    15         link = new LinkedList();
    16     }
    17 
    18     public void add(Object obj) {
    19         link.addFirst(obj);
    20     }
    21 
    22     public Object get() {
    23         // return link.getFirst();
    24         return link.removeFirst();
    25     }
    26 
    27     public boolean isEmpty() {
    28         return link.isEmpty();
    29     }
    30 }
     1 package cn.itcast_05;
     2 
     3 /*
     4  * MyStack的测试
     5  */
     6 public class MyStackDemo {
     7     public static void main(String[] args) {
     8         // 创建集合对象
     9         MyStack ms = new MyStack();
    10 
    11         // 添加元素
    12         ms.add("hello");
    13         ms.add("world");
    14         ms.add("java");
    15 
    16         // System.out.println(ms.get());
    17         // System.out.println(ms.get());
    18         // System.out.println(ms.get());
    19         // NoSuchElementException
    20         // System.out.println(ms.get());
    21         
    22         while(!ms.isEmpty()){
    23             System.out.println(ms.get());
    24         }
    25     }
    26 }

    7、泛型概述和基本使用

     1 package cn.itcast_01;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /*
     7  * ArrayList存储字符串并遍历
     8  * 
     9  * 我们按照正常的写法来写这个程序, 结果确出错了。
    10  * 为什么呢?
    11  *         因为我们开始存储的时候,存储了String和Integer两种类型的数据。
    12  *         而在遍历的时候,我们把它们都当作String类型处理的,做了转换,所以就报错了。
    13  * 但是呢,它在编译期间却没有告诉我们。
    14  * 所以,我就觉得这个设计的不好。
    15  * 回想一下,我们的数组
    16  *         String[] strArray = new String[3];
    17  *         strArray[0] = "hello";
    18  *         strArray[1] = "world";
    19  *         strArray[2] = 10;
    20  * 集合也模仿着数组的这种做法,在创建对象的时候明确元素的数据类型。这样就不会在有问题了。
    21  * 而这种技术被称为:泛型。
    22  * 
    23  * 泛型:是一种把类型明确的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型。参数化类型,把类型当作参数一样的传递。
    24  * 格式:
    25  *         <数据类型>
    26  *         此处的数据类型只能是引用类型。
    27  * 好处:
    28  *         A:把运行时期的问题提前到了编译期间
    29  *         B:避免了强制类型转换
    30  *         C:优化了程序设计,解决了黄色警告线
    31  */
    32 public class GenericDemo {
    33     public static void main(String[] args) {
    34         // 创建
    35         ArrayList<String> array = new ArrayList<String>();
    36 
    37         // 添加元素
    38         array.add("hello");
    39         array.add("world");
    40         array.add("java");
    41         // array.add(new Integer(100));
    42         //array.add(10); // JDK5以后的自动装箱
    43         // 等价于:array.add(Integer.valueOf(10));
    44 
    45         // 遍历
    46         Iterator<String> it = array.iterator();
    47         while (it.hasNext()) {
    48             // ClassCastException
    49             // String s = (String) it.next();
    50             String s = it.next();
    51             System.out.println(s);
    52         }
    53 
    54         // 看下面这个代码
    55         // String[] strArray = new String[3];
    56         // strArray[0] = "hello";
    57         // strArray[1] = "world";
    58         // strArray[2] = 10;
    59     }
    60 }

    8、ArrayList存储字符串并遍历泛型版

     1 package cn.itcast_02;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /*
     7  * 泛型在哪些地方使用呢?
     8  *         看API,如果类,接口,抽象类后面跟的有<E>就说要使用泛型。一般来说就是在集合中使用。
     9  */
    10 public class ArrayListDemo {
    11     public static void main(String[] args) {
    12         // 用ArrayList存储字符串元素,并遍历。用泛型改进代码
    13         ArrayList<String> array = new ArrayList<String>();
    14 
    15         array.add("hello");
    16         array.add("world");
    17         array.add("java");
    18 
    19         Iterator<String> it = array.iterator();
    20         while (it.hasNext()) {
    21             String s = it.next();
    22             System.out.println(s);
    23         }
    24         System.out.println("-----------------");
    25 
    26         for (int x = 0; x < array.size(); x++) {
    27             String s = array.get(x);
    28             System.out.println(s);
    29         }
    30     }
    31 }

    9、ArrayList存储自定义对象并遍历泛型版

     1 package cn.itcast_02;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /*
     7  * 需求:存储自定义对象并遍历。
     8  * 
     9  * A:创建学生类
    10  * B:创建集合对象
    11  * C:创建元素对象
    12  * D:把元素添加到集合
    13  * E:遍历集合
    14  */
    15 public class ArrayListDemo2 {
    16     public static void main(String[] args) {
    17         // 创建集合对象
    18         // JDK7的新特性:泛型推断。
    19         // ArrayList<Student> array = new ArrayList<>();
    20         // 但是我不建议这样使用。
    21         ArrayList<Student> array = new ArrayList<Student>();
    22 
    23         // 创建元素对象
    24         Student s1 = new Student("曹操", 40); // 后知后觉
    25         Student s2 = new Student("蒋干", 30); // 不知不觉
    26         Student s3 = new Student("诸葛亮", 26);// 先知先觉
    27 
    28         // 添加元素
    29         array.add(s1);
    30         array.add(s2);
    31         array.add(s3);
    32 
    33         // 遍历
    34         Iterator<Student> it = array.iterator();
    35         while (it.hasNext()) {
    36             Student s = it.next();
    37             System.out.println(s.getName() + "---" + s.getAge());
    38         }
    39         System.out.println("------------------");
    40 
    41         for (int x = 0; x < array.size(); x++) {
    42             Student s = array.get(x);
    43             System.out.println(s.getName() + "---" + s.getAge());
    44         }
    45     }
    46 }

    10、通过Object转型问题引入泛型

     1 package cn.itcast_03;
     2 
     3 public class ObjectTool {
     4     private Object obj;
     5 
     6     public Object getObj() {
     7         return obj;
     8     }
     9 
    10     public void setObj(Object obj) { // Object obj = new Integer(30);
    11         this.obj = obj;
    12     }
    13 }
     1 package cn.itcast_03;
     2 
     3 /*
     4  * 早期的时候,我们使用Object来代表任意的类型。
     5  * 向上转型是没有任何问题的,但是在向下转型的时候其实隐含了类型转换的问题。
     6  * 也就是说这样的程序其实并不是安全的。所以Java在JDK5后引入了泛型,提高程序的安全性。
     7  */
     8 public class ObjectToolDemo {
     9     public static void main(String[] args) {
    10         ObjectTool ot = new ObjectTool();
    11 
    12         // 正常使用
    13         ot.setObj(new Integer(27));
    14         Integer i = (Integer) ot.getObj();
    15         System.out.println("年龄是:" + i);
    16 
    17         ot.setObj(new String("林青霞"));
    18         String s = (String) ot.getObj();
    19         System.out.println("姓名是:" + s);
    20 
    21         System.out.println("---------");
    22         ot.setObj(new Integer(30));
    23         // ClassCastException
    24         String ss = (String) ot.getObj();
    25         System.out.println("姓名是:" + ss);
    26     }
    27 }

    11、泛型类的概述及使用

     1 package cn.itcast_04;
     2 
     3 /*
     4  * 泛型类:把泛型定义在类上
     5  */
     6 public class ObjectTool<T> {
     7     private T obj;
     8 
     9     public T getObj() {
    10         return obj;
    11     }
    12 
    13     public void setObj(T obj) {
    14         this.obj = obj;
    15     }
    16 }
     1 package cn.itcast_04;
     2 
     3 /*
     4  * 泛型类的测试
     5  */
     6 public class ObjectToolDemo {
     7     public static void main(String[] args) {
     8         // ObjectTool ot = new ObjectTool();
     9         //
    10         // ot.setObj(new String("风清扬"));
    11         // String s = (String) ot.getObj();
    12         // System.out.println("姓名是:" + s);
    13         //
    14         // ot.setObj(new Integer(30));
    15         // Integer i = (Integer) ot.getObj();
    16         // System.out.println("年龄是:" + i);
    17 
    18         // ot.setObj(new String("林青霞"));
    19         // // ClassCastException
    20         // Integer ii = (Integer) ot.getObj();
    21         // System.out.println("姓名是:" + ii);
    22 
    23         System.out.println("-------------");
    24 
    25         ObjectTool<String> ot = new ObjectTool<String>();
    26         // ot.setObj(new Integer(27)); //这个时候编译期间就过不去
    27         ot.setObj(new String("林青霞"));
    28         String s = ot.getObj();
    29         System.out.println("姓名是:" + s);
    30 
    31         ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
    32         // ot2.setObj(new String("风清扬"));//这个时候编译期间就过不去
    33         ot2.setObj(new Integer(27));
    34         Integer i = ot2.getObj();
    35         System.out.println("年龄是:" + i);
    36     }
    37 }

    12、泛型方法的概述和使用

     1 package cn.itcast_05;
     2 
     3 //public class ObjectTool<T> {
     4 //    // public void show(String s) {
     5 //    // System.out.println(s);
     6 //    // }
     7 //    //
     8 //    // public void show(Integer i) {
     9 //    // System.out.println(i);
    10 //    // }
    11 //    //
    12 //    // public void show(Boolean b) {
    13 //    // System.out.println(b);
    14 //    // }
    15 //
    16 //    public void show(T t) {
    17 //        System.out.println(t);
    18 //    }
    19 // }
    20 
    21 /*
    22  * 泛型方法:把泛型定义在方法上
    23  */
    24 public class ObjectTool {
    25     public <T> void show(T t) {
    26         System.out.println(t);
    27     }
    28 }
    package cn.itcast_05;
    
    public class ObjectToolDemo {
        public static void main(String[] args) {
            // ObjectTool ot = new ObjectTool();
            // ot.show("hello");
            // ot.show(100);
            // ot.show(true);
    
            // ObjectTool<String> ot = new ObjectTool<String>();
            // ot.show("hello");
            //
            // ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
            // ot2.show(100);
            //
            // ObjectTool<Boolean> ot3 = new ObjectTool<Boolean>();
            // ot3.show(true);
    
            // 如果还听得懂,那就说明泛型类是没有问题的
            // 但是呢,谁说了我的方法一定要和类的类型的一致呢?
            // 我要是类上没有泛型的话,方法还能不能接收任意类型的参数了呢?
    
            // 定义泛型方法后
            ObjectTool ot = new ObjectTool();
            ot.show("hello");
            ot.show(100);
            ot.show(true);
        }
    }

    13、泛型接口的概述和使用

    1 package cn.itcast_06;
    2 
    3 /*
    4  * 泛型接口:把泛型定义在接口上
    5  */
    6 public interface Inter<T> {
    7     public abstract void show(T t);
    8 }
     1 package cn.itcast_06;
     2 
     3 //实现类在实现接口的时候
     4 //第一种情况:已经知道该是什么类型的了
     5 
     6 //public class InterImpl implements Inter<String> {
     7 //
     8 //    @Override
     9 //    public void show(String t) {
    10 //        System.out.println(t);
    11 //    }
    12 // }
    13 
    14 //第二种情况:还不知道是什么类型的
    15 public class InterImpl<T> implements Inter<T> {
    16 
    17     @Override
    18     public void show(T t) {
    19         System.out.println(t);
    20     }
    21 }
     1 package cn.itcast_06;
     2 
     3 public class InterDemo {
     4     public static void main(String[] args) {
     5         // 第一种情况的测试
     6         // Inter<String> i = new InterImpl();
     7         // i.show("hello");
     8 
     9         // // 第二种情况的测试
    10         Inter<String> i = new InterImpl<String>();
    11         i.show("hello");
    12 
    13         Inter<Integer> ii = new InterImpl<Integer>();
    14         ii.show(100);
    15     }
    16 }

    14、泛型高级之通配符

     1 package cn.itcast_07;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collection;
     5 
     6 /*
     7  * 泛型高级(通配符)
     8  * ?:任意类型,如果没有明确,那么就是Object以及任意的Java类了
     9  * ? extends E:向下限定,E及其子类
    10  * ? super E:向上限定,E极其父类
    11  */
    12 public class GenericDemo {
    13     public static void main(String[] args) {
    14         // 泛型如果明确的写的时候,前后必须一致
    15         Collection<Object> c1 = new ArrayList<Object>();
    16         // Collection<Object> c2 = new ArrayList<Animal>();
    17         // Collection<Object> c3 = new ArrayList<Dog>();
    18         // Collection<Object> c4 = new ArrayList<Cat>();
    19 
    20         // ?表示任意的类型都是可以的
    21         Collection<?> c5 = new ArrayList<Object>();
    22         Collection<?> c6 = new ArrayList<Animal>();
    23         Collection<?> c7 = new ArrayList<Dog>();
    24         Collection<?> c8 = new ArrayList<Cat>();
    25 
    26         // ? extends E:向下限定,E及其子类
    27         // Collection<? extends Animal> c9 = new ArrayList<Object>();
    28         Collection<? extends Animal> c10 = new ArrayList<Animal>();
    29         Collection<? extends Animal> c11 = new ArrayList<Dog>();
    30         Collection<? extends Animal> c12 = new ArrayList<Cat>();
    31 
    32         // ? super E:向上限定,E极其父类
    33         Collection<? super Animal> c13 = new ArrayList<Object>();
    34         Collection<? super Animal> c14 = new ArrayList<Animal>();
    35         // Collection<? super Animal> c15 = new ArrayList<Dog>();
    36         // Collection<? super Animal> c16 = new ArrayList<Cat>();
    37     }
    38 }
    39 
    40 class Animal {
    41 }
    42 
    43 class Dog extends Animal {
    44 }
    45 
    46 class Cat extends Animal {
    47 }

    15、增强for的概述和使用

     1 package cn.itcast_01;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 /*
     7  * JDK5的新特性:自动拆装箱,泛型,增强for,静态导入,可变参数,枚举
     8  * 
     9  * 增强for:是for循环的一种。
    10  * 
    11  * 格式:
    12  *         for(元素数据类型 变量 : 数组或者Collection集合) {
    13  *            使用变量即可,该变量就是元素
    14  *       }
    15  *   
    16  * 好处:简化了数组和集合的遍历。
    17  * 
    18  * 弊端: 增强for的目标不能为null。
    19  * 如何解决呢?对增强for的目标先进行不为null的判断,然后在使用。
    20  */
    21 public class ForDemo {
    22     public static void main(String[] args) {
    23         // 定义一个int数组
    24         int[] arr = { 1, 2, 3, 4, 5 };
    25         for (int x = 0; x < arr.length; x++) {
    26             System.out.println(arr[x]);
    27         }
    28         System.out.println("---------------");
    29         // 增强for
    30         for (int x : arr) {
    31             System.out.println(x);
    32         }
    33         System.out.println("---------------");
    34         // 定义一个字符串数组
    35         String[] strArray = { "林青霞", "风清扬", "东方不败", "刘意" };
    36         // 增强for
    37         for (String s : strArray) {
    38             System.out.println(s);
    39         }
    40         System.out.println("---------------");
    41         // 定义一个集合
    42         ArrayList<String> array = new ArrayList<String>();
    43         array.add("hello");
    44         array.add("world");
    45         array.add("java");
    46         // 增强for
    47         for (String s : array) {
    48             System.out.println(s);
    49         }
    50         System.out.println("---------------");
    51 
    52         List<String> list = null;
    53         // NullPointerException
    54         // 这个s是我们从list里面获取出来的,在获取前,它肯定还好做一个判断
    55         // 说白了,这就是迭代器的功能
    56         if (list != null) {
    57             for (String s : list) {
    58                 System.out.println(s);
    59             }
    60         }
    61 
    62         // 增强for其实是用来替代迭代器的
    63         //ConcurrentModificationException
    64         // for (String s : array) {
    65         // if ("world".equals(s)) {
    66         // array.add("javaee");
    67         // }
    68         // }
    69         // System.out.println("array:" + array);
    70     }
    71 }

     练习:ArrayList存储字符串并遍历增强for版

     1 package cn.itcast_01;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /*
     7  * ArrayList存储字符串并遍历。要求加入泛型,并用增强for遍历。
     8  * A:迭代器
     9  * B:普通for
    10  * C:增强for
    11  */
    12 public class ArrayListDemo {
    13     public static void main(String[] args) {
    14         // 创建集合对象
    15         ArrayList<String> array = new ArrayList<String>();
    16 
    17         // 创建并添加元素
    18         array.add("hello");
    19         array.add("world");
    20         array.add("java");
    21 
    22         // 遍历集合
    23         // 迭代器
    24         Iterator<String> it = array.iterator();
    25         while (it.hasNext()) {
    26             String s = it.next();
    27             System.out.println(s);
    28         }
    29         System.out.println("------------------");
    30 
    31         // 普通for
    32         for (int x = 0; x < array.size(); x++) {
    33             String s = array.get(x);
    34             System.out.println(s);
    35         }
    36         System.out.println("------------------");
    37 
    38         // 增强for
    39         for (String s : array) {
    40             System.out.println(s);
    41         }
    42     }
    43 }

    练习:ArrayList存储自定义对象并遍历增强for版

     1 package cn.itcast_01;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Iterator;
     5 
     6 /*
     7  * 需求:ArrayList存储自定义对象并遍历。要求加入泛型,并用增强for遍历。
     8  * A:迭代器
     9  * B:普通for
    10  * C:增强for
    11  * 
    12  * LinkedList,Vector,Colleciton,List等存储我还讲吗?不讲解了,但是要求你们练习。
    13  * 
    14  * 增强for是用来替迭代器。
    15  */
    16 public class ArrayListDemo2 {
    17     public static void main(String[] args) {
    18         // 创建集合对象
    19         ArrayList<Student> array = new ArrayList<Student>();
    20 
    21         // 创建学生对象
    22         Student s1 = new Student("林青霞", 27);
    23         Student s2 = new Student("貂蝉", 22);
    24         Student s3 = new Student("杨玉环", 24);
    25         Student s4 = new Student("西施", 21);
    26         Student s5 = new Student("王昭君", 23);
    27 
    28         // 把学生对象添加到集合中
    29         array.add(s1);
    30         array.add(s2);
    31         array.add(s3);
    32         array.add(s4);
    33         array.add(s5);
    34 
    35         // 迭代器
    36         Iterator<Student> it = array.iterator();
    37         while (it.hasNext()) {
    38             Student s = it.next();
    39             System.out.println(s.getName() + "---" + s.getAge());
    40         }
    41         System.out.println("---------------");
    42 
    43         // 普通for
    44         for (int x = 0; x < array.size(); x++) {
    45             Student s = array.get(x);
    46             System.out.println(s.getName() + "---" + s.getAge());
    47         }
    48         System.out.println("---------------");
    49 
    50         // 增强for
    51         for (Student s : array) {
    52             System.out.println(s.getName() + "---" + s.getAge());
    53         }
    54     }
    55 }

    16、静态导入的概述和使用

     1 package cn.itcast_02;
     2 
     3 /*
     4  * 静态导入:
     5  * 格式:import static 包名….类名.方法名;
     6  * 可以直接导入到方法的级别
     7  * 
     8  * 静态导入的注意事项:
     9  *         A:方法必须是静态的
    10  *         B:如果有多个同名的静态方法,容易不知道使用谁?这个时候要使用,必须加前缀。由此可见,意义不大,所以一般不用,但是要能看懂。
    11  */
    12 import static java.lang.Math.abs;
    13 import static java.lang.Math.pow;
    14 import static java.lang.Math.max;
    15 
    16 //错误
    17 //import static java.util.ArrayList.add;
    18 
    19 public class StaticImportDemo {
    20     public static void main(String[] args) {
    21         // System.out.println(java.lang.Math.abs(-100));
    22         // System.out.println(java.lang.Math.pow(2, 3));
    23         // System.out.println(java.lang.Math.max(20, 30));
    24         // 太复杂,我们就引入到import
    25 
    26         // System.out.println(Math.abs(-100));
    27         // System.out.println(Math.pow(2, 3));
    28         // System.out.println(Math.max(20, 30));
    29         // 太复杂,有更简单
    30 
    31 //        System.out.println(abs(-100));
    32         System.out.println(java.lang.Math.abs(-100));
    33         System.out.println(pow(2, 3));
    34         System.out.println(max(20, 30));
    35     }
    36     
    37     public static void abs(String s){
    38         System.out.println(s);
    39     }
    40 }

    17、可变参数的概述和使用

     1 package cn.itcast_03;
     2 
     3 /*
     4  * 可变参数:定义方法的时候不知道该定义多少个参数
     5  * 格式:
     6  *         修饰符 返回值类型 方法名(数据类型…  变量名){
     7  * 
     8  *         }
     9  * 
    10  *         注意:
    11  *             这里的变量其实是一个数组
    12  *             如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
    13  */                
    14 public class ArgsDemo {
    15     public static void main(String[] args) {
    16         // 2个数据求和
    17         int a = 10;
    18         int b = 20;
    19         int result = sum(a, b);
    20         System.out.println("result:" + result);
    21 
    22         // 3个数据的求和
    23         int c = 30;
    24         result = sum(a, b, c);
    25         System.out.println("result:" + result);
    26 
    27         // 4个数据的求和
    28         int d = 30;
    29         result = sum(a, b, c, d);
    30         System.out.println("result:" + result);
    31 
    32         // 需求:我要写一个求和的功能,到底是几个数据求和呢,我不太清楚,但是我知道在调用的时候我肯定就知道了
    33         // 为了解决这个问题,Java就提供了一个东西:可变参数
    34         result = sum(a, b, c, d, 40);
    35         System.out.println("result:" + result);
    36 
    37         result = sum(a, b, c, d, 40, 50);
    38         System.out.println("result:" + result);
    39     }
    40 
    41     public static int sum(int... a) {
    42         // System.out.println(a);
    43         //return 0;
    44 
    45         int s = 0;
    46         
    47         for(int x : a){
    48             s +=x;
    49         }
    50         
    51         return s;
    52     }
    53 
    54     // public static int sum(int a, int b, int c, int d) {
    55     // return a + b + c + d;
    56     // }
    57     //
    58     // public static int sum(int a, int b, int c) {
    59     // return a + b + c;
    60     // }
    61     //
    62     // public static int sum(int a, int b) {
    63     // return a + b;
    64     // }
    65 }

    18、Arrays工具类的asList()方法的使用

     1 package cn.itcast_03;
     2 
     3 import java.util.Arrays;
     4 import java.util.List;
     5 
     6 /*
     7  * public static <T> List<T> asList(T... a):把数组转成集合
     8  * 
     9  * 注意事项:
    10  *         虽然可以把数组转成集合,但是集合的长度不能改变。
    11  */
    12 public class ArraysDemo {
    13     public static void main(String[] args) {
    14         // 定义一个数组
    15         // String[] strArray = { "hello", "world", "java" };
    16         // List<String> list = Arrays.asList(strArray);
    17 
    18         List<String> list = Arrays.asList("hello", "world", "java");
    19         // UnsupportedOperationException
    20         // list.add("javaee");
    21         // UnsupportedOperationException
    22         // list.remove(1);
    23         list.set(1, "javaee");
    24 
    25         for (String s : list) {
    26             System.out.println(s);
    27         }
    28     }
    29 }

    19、集合嵌套存储和遍历元素的案例图解及实现

     1 package cn.itcast_01;
     2 
     3 import java.util.ArrayList;
     4 
     5 /*
     6  * 集合的嵌套遍历
     7  * 需求:
     8  *         我们班有学生,每一个学生是不是一个对象。所以我们可以使用一个集合表示我们班级的学生。ArrayList<Student>
     9  *         但是呢,我们旁边是不是还有班级,每个班级是不是也是一个ArrayList<Student>。
    10  *         而我现在有多个ArrayList<Student>。也要用集合存储,怎么办呢?
    11  *         就是这个样子的:ArrayList<ArrayList<Student>>
    12  */
    13 public class ArrayListDemo {
    14     public static void main(String[] args) {
    15         // 创建大集合
    16         ArrayList<ArrayList<Student>> bigArrayList = new ArrayList<ArrayList<Student>>();
    17 
    18         // 创建第一个班级的学生集合
    19         ArrayList<Student> firstArrayList = new ArrayList<Student>();
    20         // 创建学生
    21         Student s1 = new Student("唐僧", 30);
    22         Student s2 = new Student("孙悟空", 29);
    23         Student s3 = new Student("猪八戒", 28);
    24         Student s4 = new Student("沙僧", 27);
    25         Student s5 = new Student("白龙马", 26);
    26         // 学生进班
    27         firstArrayList.add(s1);
    28         firstArrayList.add(s2);
    29         firstArrayList.add(s3);
    30         firstArrayList.add(s4);
    31         firstArrayList.add(s5);
    32         // 把第一个班级存储到学生系统中
    33         bigArrayList.add(firstArrayList);
    34 
    35         // 创建第二个班级的学生集合
    36         ArrayList<Student> secondArrayList = new ArrayList<Student>();
    37         // 创建学生
    38         Student s11 = new Student("诸葛亮", 30);
    39         Student s22 = new Student("司马懿", 28);
    40         Student s33 = new Student("周瑜", 26);
    41         // 学生进班
    42         secondArrayList.add(s11);
    43         secondArrayList.add(s22);
    44         secondArrayList.add(s33);
    45         // 把第二个班级存储到学生系统中
    46         bigArrayList.add(secondArrayList);
    47 
    48         // 创建第三个班级的学生集合
    49         ArrayList<Student> thirdArrayList = new ArrayList<Student>();
    50         // 创建学生
    51         Student s111 = new Student("宋江", 40);
    52         Student s222 = new Student("吴用", 35);
    53         Student s333 = new Student("高俅", 30);
    54         Student s444 = new Student("李师师", 22);
    55         // 学生进班
    56         thirdArrayList.add(s111);
    57         thirdArrayList.add(s222);
    58         thirdArrayList.add(s333);
    59         thirdArrayList.add(s444);
    60         // 把第三个班级存储到学生系统中
    61         bigArrayList.add(thirdArrayList);
    62 
    63         // 遍历集合
    64         for (ArrayList<Student> array : bigArrayList) {
    65             for (Student s : array) {
    66                 System.out.println(s.getName() + "---" + s.getAge());
    67             }
    68         }
    69     }
    70 }

     练习:产生10个1-20之间的随机数要求随机数不能重复案例

     1 package cn.itcast_02;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Random;
     5 
     6 /*
     7  * 获取10个1-20之间的随机数,要求不能重复
     8  * 
     9  * 用数组实现,但是数组的长度是固定的,长度不好确定。
    10  * 所以我们使用集合实现。
    11  * 
    12  * 分析:
    13  *         A:创建产生随机数的对象
    14  *         B:创建一个存储随机数的集合。
    15  *         C:定义一个统计变量。从0开始。
    16  *         D:判断统计遍历是否小于10
    17  *             是:先产生一个随机数,判断该随机数在集合中是否存在。
    18  *                     如果不存在:就添加,统计变量++。
    19  *                     如果存在:就不搭理它。
    20  *             否:不搭理它
    21  *         E:遍历集合
    22  */
    23 public class RandomDemo {
    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             //先产生一个随机数
    37             int number = r.nextInt(20) + 1;
    38             
    39             //判断该随机数在集合中是否存在。
    40             if(!array.contains(number)){
    41                 //如果不存在:就添加,统计变量++。
    42                 array.add(number);
    43                 count++;
    44             }
    45         }
    46         
    47         //遍历集合
    48         for(Integer i : array){
    49             System.out.println(i);
    50         }
    51     }
    52 }

    练习:键盘录入多个数据在控制台输出最大值案例

     1 package cn.itcast_03;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Arrays;
     5 import java.util.Scanner;
     6 
     7 /*
     8  * 键盘录入多个数据,以0结束,要求在控制台输出这多个数据中的最大值
     9  * 
    10  * 分析:
    11  *         A:创建键盘录入数据对象
    12  *         B:键盘录入多个数据,我们不知道多少个,所以用集合存储
    13  *         C:以0结束,这个简单,只要键盘录入的数据是0,我就不继续录入数据了
    14  *         D:把集合转成数组
    15  *         E:对数组排序
    16  *         F:获取该数组中的最大索引的值
    17  */
    18 public class ArrayListDemo {
    19     public static void main(String[] args) {
    20         // 创建键盘录入数据对象
    21         Scanner sc = new Scanner(System.in);
    22 
    23         // 键盘录入多个数据,我们不知道多少个,所以用集合存储
    24         ArrayList<Integer> array = new ArrayList<Integer>();
    25 
    26         // 以0结束,这个简单,只要键盘录入的数据是0,我就不继续录入数据了
    27         while (true) {
    28             System.out.println("请输入数据:");
    29             int number = sc.nextInt();
    30             if (number != 0) {
    31                 array.add(number);
    32             } else {
    33                 break;
    34             }
    35         }
    36 
    37         // 把集合转成数组
    38         // public <T> T[] toArray(T[] a)
    39         Integer[] i = new Integer[array.size()];
    40         // Integer[] ii = array.toArray(i);
    41         array.toArray(i);
    42         // System.out.println(i);
    43         // System.out.println(ii);
    44 
    45         // 对数组排序
    46         // public static void sort(Object[] a)
    47         Arrays.sort(i);
    48 
    49         // 获取该数组中的最大索引的值
    50         System.out.println("数组是:" + arrayToString(i) + "最大值是:"
    51                 + i[i.length - 1]);
    52     }
    53 
    54     public static String arrayToString(Integer[] i) {
    55         StringBuilder sb = new StringBuilder();
    56 
    57         sb.append("[");
    58         for (int x = 0; x < i.length; x++) {
    59             if (x == i.length - 1) {
    60                 sb.append(i[x]);
    61             } else {
    62                 sb.append(i[x]).append(", ");
    63             }
    64         }
    65         sb.append("]");
    66 
    67         return sb.toString();
    68     }
    69 }
    如欢如殇 授以青春鲜活肢体奔忙 如思如忘 驱以老朽深沉灵魂冥想 始自情热激荡 从未敢终于世事炎凉 无能执手相望 无法去尝试结发同床 无力至心死身僵 一息坚强 ------ 我一直没有放弃,如果你也能看到 修身 修禅
  • 相关阅读:
    hdu1686 最大匹配次数 KMP
    洛谷 P5057 [CQOI2006]简单题(树状数组)
    洛谷 P5020 货币系统
    洛谷 P5019 铺设道路(差分)
    洛谷 P1119 灾后重建(Floyd)
    洛谷 P1082 同余方程(同余&&exgcd)
    洛谷 P2384 最短路
    洛谷 P3371 【模板】单源最短路径(弱化版) && dijkstra模板
    洛谷 P1387 最大正方形
    洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day
  • 原文地址:https://www.cnblogs.com/lz2lhy/p/6920355.html
Copyright © 2011-2022 走看看