- 浏览: 215662 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
junzi2013:
df
黑马程序员:MyEclipse9 常用快捷键 中英文对照 及 快速get set方法 -
junzi2013:
dfsdafadsfsd
黑马程序员:MyEclipse9 常用快捷键 中英文对照 及 快速get set方法 -
idealab:
博主说的OpenCSV, JavaCSV, SuperCSV都 ...
csv调研 -
tanxin:
楼主怎么解决这个问题的呢?
坑爹的zookeer -
yy22258867:
:twisted:
黑马程序员19-7:foreach遍历核心源码,遍历HashMap需要用map.keySet()或map.entrySet()
package cn.itcast.p5.generic.advance.demo; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; public class GenericAdvanceDemo { /** * @param args */ public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("abc"); al.add("hehe"); ArrayList<Integer> al2 = new ArrayList<Integer>(); al2.add(5); al2.add(67); printCollection(al); printCollection(al2); } /** * 迭代并打印集合中元素。 * @param al */ //!!!接受任意类型参数的集合方法写法,通配符,printCollection(Collection<?> al) public static void printCollection(Collection<?> al) { Iterator<?> it = al.iterator(); while(it.hasNext()){ // T str = it.next(); // System.out.println(str); System.out.println(it.next().toString()); } } }
package cn.itcast.p5.generic.advance.demo; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import cn.itcast.p2.bean.Person; import cn.itcast.p2.bean.Student; import cn.itcast.p2.bean.Worker; public class GenericAdvanceDemo2 { /** * @param args */ public static void main(String[] args) { ArrayList<Person> al = new ArrayList<Person>(); al.add(new Person("abc",30)); al.add(new Person("abc4",34)); ArrayList<Student> al2 = new ArrayList<Student>(); al2.add(new Student("stu1",11)); al2.add(new Student("stu2",22)); ArrayList<String> al3 = new ArrayList<String>(); al3.add("stu3331"); al3.add("stu33332"); printCollection(al2); printCollection(al); } /** * 迭代并打印集合中元素。 * * 可以对类型进行限定: * ? extends E:接收E类型或者E的子类型对象。上限(上面封顶,从封顶处无限往下)!,用的多 * * ? super E :接收E类型或者E的父类型。下限(下面封顶,从封顶处无限往上)! * @param al */ //!!!!!!!只接受继承某父类的子类,其他的都不接受,这样做的好处可以对接受类型进行限定 //如果写extends Object就可以接受所有的类型 /*public static void printCollection(Collection<? extends Person> al) {//Collection<Dog> al = new ArrayList<Dog>() Iterator<? extends Person> it = al.iterator(); while(it.hasNext()){ // T str = it.next(); // System.out.println(str); // System.out.println(it.next().toString()); Person p = it.next(); System.out.println(p.getName()+":"+p.getAge()); } }*/ //!!!!只能接受自己类型和父类 public static void printCollection(Collection<? super Student> al){ Iterator<? super Student> it = al.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } }
package cn.itcast.p5.generic.advance.demo; import java.util.ArrayList; import cn.itcast.p2.bean.Person; import cn.itcast.p2.bean.Student; import cn.itcast.p2.bean.Worker; public class GenericAdvanceDemo3 { /** * @param args */ public static void main(String[] args) { ArrayList<Person> al1 = new ArrayList<Person>(); al1.add(new Person("abc",30)); al1.add(new Person("abc4",34)); ArrayList<Student> al2 = new ArrayList<Student>(); al2.add(new Student("stu1",11)); al2.add(new Student("stu2",22)); ArrayList<Worker> al3 = new ArrayList<Worker>(); al3.add(new Worker("stu1",11)); al3.add(new Worker("stu2",22)); ArrayList<String> al4 = new ArrayList<String>(); al4.add("abcdeef"); // al1.addAll(al4);//错误,类型不匹配。 al1.addAll(al2); al1.addAll(al3); System.out.println(al1.size()); // printCollection(al2); // printCollection(al); } } /* * 一般在存储元素的时候都是用上限,因为这样取出都是按照上限类型来运算的。不会出现类型安全隐患。 * */ class MyCollection<E>{ public void add(E e){ } //!!!能够接受E及其子类 public void addAll(MyCollection<? extends E> e){ } }
package cn.itcast.p5.generic.advance.demo; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; import cn.itcast.p2.bean.Person; import cn.itcast.p2.bean.Student; import cn.itcast.p2.bean.Worker; public class GenericAdvanceDemo4 { /** * @param args */ public static void main(String[] args) { TreeSet<Person> al1 = new TreeSet<Person>(new CompByName()); al1.add(new Person("abc4",34)); al1.add(new Person("abc1",30)); al1.add(new Person("abc2",38)); //!!!!!默认构造方法中,Tree(Comparator<? super Student> comp);因此,可以用与Person一致的方法接受 TreeSet<Student> al2 = new TreeSet<Student>(new CompByName()); al2.add(new Student("stu1",11)); al2.add(new Student("stu7",20)); al2.add(new Student("stu2",22)); TreeSet<Worker> al3 = new TreeSet<Worker>(); al3.add(new Worker("stu1",11)); al3.add(new Worker("stu2",22)); TreeSet<String> al4 = new TreeSet<String>(); al4.add("abcdeef"); // al1.addAll(al4);//错误,类型不匹配。 // al1.addAll(al2); // al1.addAll(al3); // System.out.println(al1.size()); Iterator<Student> it = al2.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } } /* * class TreeSet<Worker> * { * Tree(Comparator<? super Worker> comp); * } * !!!重点看这个TreeSet中有这个构造函数用来比较,因此具备了Comparator直接使用父类接受一切子类的情况 * 什么时候用下限呢?通常对集合中的元素进行取出操作时,可以是用下限。 * */ class CompByName implements Comparator<Person>{ @Override public int compare(Person o1, Person o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } } class CompByStuName implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } } class CompByWorkerName implements Comparator<Worker>{ @Override public int compare(Worker o1, Worker o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } }
package cn.itcast.p5.generic.advance.demo; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; import cn.itcast.p2.bean.Person; import cn.itcast.p2.bean.Student; import cn.itcast.p2.bean.Worker; public class GenericAdvanceDemo4 { /** * @param args */ public static void main(String[] args) { TreeSet<Person> al1 = new TreeSet<Person>(new CompByName()); al1.add(new Person("abc4",34)); al1.add(new Person("abc1",30)); al1.add(new Person("abc2",38)); //!!!!!默认构造方法中,Tree(Comparator<? super Student> comp);因此,可以用与Person一致的方法接受 TreeSet<Student> al2 = new TreeSet<Student>(new CompByName()); al2.add(new Student("stu1",11)); al2.add(new Student("stu7",20)); al2.add(new Student("stu2",22)); TreeSet<Worker> al3 = new TreeSet<Worker>(); al3.add(new Worker("stu1",11)); al3.add(new Worker("stu2",22)); TreeSet<String> al4 = new TreeSet<String>(); al4.add("abcdeef"); // al1.addAll(al4);//错误,类型不匹配。 // al1.addAll(al2); // al1.addAll(al3); // System.out.println(al1.size()); Iterator<Student> it = al2.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } } /* * class TreeSet<Worker> * { * Tree(Comparator<? super Worker> comp); * } * !!!重点看这个TreeSet中有这个构造函数用来比较,因此具备了Comparator直接使用父类接受一切子类的情况 * 什么时候用下限呢?通常对集合中的元素进行取出操作时,可以是用下限。 * */ class CompByName implements Comparator<Person>{ @Override public int compare(Person o1, Person o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } } class CompByStuName implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } } class CompByWorkerName implements Comparator<Worker>{ @Override public int compare(Worker o1, Worker o2) { int temp = o1.getName().compareTo(o2.getName()); return temp==0? o1.getAge()-o2.getAge():temp; } }
详细请查看:http://edu.csdn.net/heima -------
发表评论
-
Java中的IO整理完整版
2013-02-15 17:23 657http://developer.51cto.com/art/ ... -
Myeclipse中导入zip文件查看源代码
2012-09-21 23:35 999用Myeclipse的时候,出现无法查看源码的话,导入jdk文 ... -
==与equals
2012-09-13 20:15 593一般基本数据类型的比较用 == 当涉及到对象、字符串的时候,就 ... -
多线程课程002:线程范围内的共享变量
2012-09-13 16:47 904package cn.itcast.heima2; ... -
多线程课程001:线程安全的问题
2012-09-13 14:37 771下面的做法,会发现输出结果被打断了。 package co ... -
多线程课程001:线程安全的问题
2012-09-13 11:14 750package com.lee.thread; pu ... -
内部类的实际应用
2012-09-13 11:01 616public class TraditionalThreadS ... -
Java接口
2012-09-11 12:07 720Java接口的方法只能是抽象的和公开的,Java接口不能有构造 ... -
移位运算符
2012-09-11 09:31 803移位运算符 <<左移 a<<b,a转 ... -
【总结】gridbaglayout布局管理器
2012-08-15 11:19 658http://blog.sina.com.cn/s/blog_ ... -
【练习_00010】java不重复随机数
2012-08-14 01:26 651package com.heima.test; impo ... -
【练习】判断一个字符串中某些字段出现的次数
2012-08-14 01:08 931package com.heima.test; im ... -
选择、冒泡、折半排序查找方法
2012-08-13 15:06 1172import java.util.Arrays; i ... -
自己写的杨辉三角
2012-08-12 16:41 763package com.lee.graphic; p ... -
交换两个数的位置,不用中间变量
2012-08-06 02:28 1073a ^= b; b ^= a; a ^= b; 1 ... -
未解问题,定时器无法自定义切换时间
2012-07-30 18:08 971package com.heima.test004.enu ... -
黑马程序员:设计模式,享元模式flyweight
2012-07-23 02:13 0如果有很多很小的东西,对象,当他们有很多属性是相同的,就可以把 ... -
黑马程序员:for综合图形整合结晶
2012-07-23 01:16 771package Graph; public clas ... -
JS: js获取字符串长度
2012-07-20 18:01 0<SCRIPT LANGUAGE="Jav ... -
传智27-2: 模拟Tocat浏览器,写网页蜘蛛爬虫,抓取网页信息
2012-07-12 17:03 0package cn.itcast.net.p2.ie_s ...
相关推荐
Java试题-3:反射和泛型的综合应用 Java反射 泛型都是比较高级的应用技术
NULL 博文链接:https://840198532-qq-com.iteye.com/blog/1490705
NULL 博文链接:https://840198532-qq-com.iteye.com/blog/1491352
泛型集合解释的非常好泛型集合泛型集合泛型集合解释的非常好泛型集合泛型集合泛型集合解释的非常好泛型集合泛型集合泛型集合解释的非常好泛型集合泛型集合
基于java的开发源码-java多线程反射泛型及正则表达式学习笔记和源码.zip 基于java的开发源码-java多线程反射泛型及正则表达式学习笔记和源码.zip 基于java的开发源码-java多线程反射泛型及正则表达式学习笔记和源码....
JAVA设计模式--程序设计--反射--注解--泛型
这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。 Java语言引入泛型的好处是安全简单。 在Java SE 1.5之前,没有泛型的情况的下,通过对类型Object的引用来实现参数的“任意化”...
类型通配符:展示如何使用类型通配符来增加灵活性,以及如何进行类型边界约束。 泛型接口:演示如何定义和实现泛型接口,并通过示例代码展示泛型接口的应用。 希望这个代码资源能够帮助你更好地理解和应用Java中的...
const-buffer:使用const泛型在堆栈上分配的固定容量的内存缓冲区
姓名Type :: Utils :: Generics-轻松创建泛型类型概要use Type::Utils::Generics qw( class_generics T );package Queue { use Moo; use Types::Standard -types; has data => ( is => 'ro', isa => ArrayRef, ...
这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。 Java语言引入泛型的好处是安全简单。 在Java SE 1.5之前,没有泛型的情况的下,通过对类型Object的引用来实现参数的“任意化”...
·课程中,Eclipse和IDEA这两种企业一线开发环境都使用到了 3.技术讲解更深入、更全面: ·课程共30天,715个知识视频小节,涉及主流Java使用的方方面面,全而不冗余 ·全程内容涵盖数据结构、设计模式、JVM内存...
学过之后对AWT事件模型、菜单项、图形绘制、集合与泛型会有更深的认识
day02_Collection、泛型 day03_List、Set、数据结构、Collections day04_Map,斗地主案例 day05_异常,线程 day06_线程、同步 day07_等待与唤醒案例、线程池、Lambda表达式 day08_File类、递归 day09_字节流、字符流...
泛型:就是在定义类,接口时指定类型形参, 这个类型形参将在声明变量,创建对象时确定(即实际传入的类型参数,也称为类型实参)
·课程中,Eclipse和IDEA这两种企业一线开发环境都使用到了 3.技术讲解更深入、更全面: ·课程共30天,715个知识视频小节,涉及主流Java使用的方方面面,全而不冗余 ·全程内容涵盖数据结构、设计模式、JVM内存...
超全面javaweb教程第7天-_07_泛型方法和泛型类的使用
泛型类型:定义泛型类型时,使用尖括号,并在尖括号中指定类型参数。例如,List表示一个字符串类型的列表。 泛型方法:定义泛型方法时,在方法名后面使用尖括号,并在尖括号中指定类型参数。例如,public static <T>...
TypedArrays PHP的泛型替代。 实现定义类型的数组。要求需要PHP> = 7.4安装composer require chemaclass/typed-arrays吉特·胡克斯(Git Hooks) 使用./tools/git-hooks/init.sh启用git钩子
03-Java集合-泛型面试题(24题)-新增