object trac 基本使用 java 类 内容 定义 pointer nim
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 // 创建集合对象
- 12ArrayList array =new ArrayList();
- 13
- 14 // 创建学生对象
- 15Student s1 =newStudent("武松", 30);
- 16Student s2 =newStudent("鲁智深", 40);
- 17Student s3 =newStudent("林冲", 36);
- 18Student s4 =newStudent("杨志", 38);
- 19
- 20 // 添加元素
- 21 array.add(s1);
- 22 array.add(s2);
- 23 array.add(s3);
- 24 array.add(s4);
- 25
- 26 // 遍历
- 27Iterator it = array.iterator();
- 28 while (it.hasNext()) {
- 29Student s = (Student) it.next();
- 30System.out.println(s.getName() + "---" + s.getAge());
- 31 }
- 32
- 33System.out.println("----------------");
- 34
- 35 for(intx = 0; x < array.size(); x++) {
- 36 // ClassCastException 注意,千万要搞清楚类型
- 37 // String s = (String) array.get(x);
- 38 // System.out.println(s);
- 39
- 40Student s = (Student) array.get(x);
- 41System.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 // 创建集合对象
- 20LinkedList link =new LinkedList();
- 21
- 22 // 添加元素
- 23link.add("hello");
- 24link.add("world");
- 25link.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()
- 38System.out.println("removeFirst:" + link.removeFirst());
- 39 // public Object removeLast()
- 40System.out.println("removeLast:" + link.removeLast());
- 41
- 42 // 输出对象名
- 43System.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 // 创建
- 35ArrayList array = newArrayList();
- 36
- 37 // 添加元素
- 38array.add("hello");
- 39array.add("world");
- 40array.add("java");
- 41 // array.add(new Integer(100));
- 42 //array.add(10); // JDK5以后的自动装箱
- 43 // 等价于:array.add(Integer.valueOf(10));
- 44
- 45 // 遍历
- 46Iterator it = array.iterator();
- 47 while (it.hasNext()) {
- 48 // ClassCastException
- 49 // String s = (String) it.next();
- 50String 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 array = new ArrayList();
- 14 15 array.add("hello");
- 16 array.add("world");
- 17 array.add("java");
- 18 19 Iterator 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 array = new ArrayList();
- 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 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) {
- 10ObjectTool ot =new ObjectTool();
- 11
- 12 // 正常使用
- 13ot.setObj(newInteger(27));
- 14Integer i = (Integer) ot.getObj();
- 15System.out.println("年龄是:" + i);
- 16
- 17ot.setObj(newString("林青霞"));
- 18String s = (String) ot.getObj();
- 19System.out.println("姓名是:" + s);
- 20
- 21System.out.println("---------");
- 22ot.setObj(newInteger(30));
- 23 // ClassCastException
- 24String ss = (String) ot.getObj();
- 25System.out.println("姓名是:" + ss);
- 26 }
- 27}
11、泛型类的概述及使用
- 1 package cn.itcast_04;
- 2 3
- /*
- 4 * 泛型类:把泛型定义在类上
- 5 */
- 6 public class ObjectTool {
- 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
- 23System.out.println("-------------");
- 24
- 25ObjectTool ot = newObjectTool();
- 26 // ot.setObj(new Integer(27)); //这个时候编译期间就过不去
- 27ot.setObj(newString("林青霞"));
- 28String s = ot.getObj();
- 29System.out.println("姓名是:" + s);
- 30
- 31ObjectTool ot2 = newObjectTool();
- 32 // ot2.setObj(new String("风清扬"));//这个时候编译期间就过不去
- 33ot2.setObj(newInteger(27));
- 34Integer i = ot2.getObj();
- 35System.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 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 {
- 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 classInterImpl implementsInter {
- 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 // // 第二种情况的测试
- 10Inter i = newInterImpl();
- 11i.show("hello");
- 12
- 13Inter ii = newInterImpl();
- 14ii.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 // 泛型如果明确的写的时候,前后必须一致
- 15Collection c1 = newArrayList();
- 16 // Collection<Object> c2 = new ArrayList<Animal>();
- 17 // Collection<Object> c3 = new ArrayList<Dog>();
- 18 // Collection<Object> c4 = new ArrayList<Cat>();
- 19
- 20 // ?表示任意的类型都是可以的
- 21Collection c5 =newArrayList();
- 22Collection c6 =newArrayList();
- 23Collection c7 =newArrayList();
- 24Collection c8 =newArrayList();
- 25
- 26 // ? extends E:向下限定,E及其子类
- 27 // Collection<? extends Animal> c9 = new ArrayList<Object>();
- 28CollectionextendsAnimal> c10 =newArrayList();
- 29CollectionextendsAnimal> c11 =newArrayList();
- 30CollectionextendsAnimal> c12 =newArrayList();
- 31
- 32 // ? super E:向上限定,E极其父类
- 33CollectionsuperAnimal> c13 =newArrayList();
- 34CollectionsuperAnimal> c14 =newArrayList();
- 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 classDogextends Animal {
- 44 }
- 45
- 46 classCatextends 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 *
来源: http://www.bubuko.com/infodetail-2095106.html