泛型在集合中的使用
2018-03-25 23:35
246 查看
1.泛型在集合中的使用(掌握)
2.自定义泛型类、泛型接口、泛型方法(理解 --->使用)
3.泛型与继承的关系
4.通配符
1.在集合中不使用泛型
public void test1(){
List list = new ArrayList();
list.add(89);
list.add(87);
list.add(67);
//1.没有使用泛型,任何Object及其子类的对象都可以添加进来
list.add(new String("AA"));
for(int i = 0;i < list.size();i++){
//2.强转为int型时,可能报ClassCastException的异常
int score = (Integer)list.get(i);
System.out.println(score);
}
}
2.在集合中使用了泛型
public void test2(){
List<Integer> list = new ArrayList<Integer>();
list.add(78);
list.add(87);
// list.add("AA");
// for(int i = 0;i < list.size();i++){
// int score = list.get(i);
// System.out.println(score);
// }
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public void test3(){
Map<String,Integer> map = new HashMap<>();
map.put("AA", 78);
map.put("BB", 87);
map.put("DD", 98);
Set<Map.Entry<String,Integer>> set = map.entrySet();
for(Map.Entry<String,Integer> o : set){
System.out.println(o.getKey() + "--->" + o.getValue());
}
}
3.自定义泛型类:应用
public class DAO<T> {
public void add(T t){
//....
}
public T get(int index){
return null;
}
public List<T> getForList(int index){
return null;
}
public void delete(int index){
}
}
public class CustomerDAO extends DAO<Customer>{
}
public class TestCustomerDAO {
public static void main(String[] args) {
CustomerDAO c = new CustomerDAO();
c.add(new Customer());
c.get(0);
}
}
【注意点】
1.对象实例化时不指定泛型,默认为:Object。
2.泛型不同的引用不能相互赋值。
3.加入集合中的对象类型必须与指定的泛型类型一致。
4.静态方法中不能使用类的泛型。
5.如果泛型类是一个接口或抽象类,则不可创建泛型
类的对象。
6.不能在catch中使用泛型
7.从泛型类派生子类,泛型类型需具体化
4.泛型与继承的关系
A类是B类的子类,G是带泛型声明的类或接口。那么G<A>不是G<B>的子类!
5.通配符:?
A类是B类的子类,G是带泛型声明的类或接口。则G<?> 是G<A>、G<B>的父类!
①以List<?>为例,能读取其中的数据。因为不管存储的是什么类型的元素,其一定是Object类的或其子类的。
①以List<?>为例,不可以向其中写入数据。因为没有指明可以存放到其中的元素的类型!唯一例外的是:null
6*. List<? extends A> :可以将List<A>的对象或List<B>的对象赋给List<? extends A>。其中B 是A的子类
? super A:可以将List<A>的对象或List<B>的对象赋给List<? extends A>。其中B 是A的父类
2.自定义泛型类、泛型接口、泛型方法(理解 --->使用)
3.泛型与继承的关系
4.通配符
1.在集合中不使用泛型
public void test1(){
List list = new ArrayList();
list.add(89);
list.add(87);
list.add(67);
//1.没有使用泛型,任何Object及其子类的对象都可以添加进来
list.add(new String("AA"));
for(int i = 0;i < list.size();i++){
//2.强转为int型时,可能报ClassCastException的异常
int score = (Integer)list.get(i);
System.out.println(score);
}
}
2.在集合中使用了泛型
public void test2(){
List<Integer> list = new ArrayList<Integer>();
list.add(78);
list.add(87);
// list.add("AA");
// for(int i = 0;i < list.size();i++){
// int score = list.get(i);
// System.out.println(score);
// }
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public void test3(){
Map<String,Integer> map = new HashMap<>();
map.put("AA", 78);
map.put("BB", 87);
map.put("DD", 98);
Set<Map.Entry<String,Integer>> set = map.entrySet();
for(Map.Entry<String,Integer> o : set){
System.out.println(o.getKey() + "--->" + o.getValue());
}
}
3.自定义泛型类:应用
public class DAO<T> {
public void add(T t){
//....
}
public T get(int index){
return null;
}
public List<T> getForList(int index){
return null;
}
public void delete(int index){
}
}
public class CustomerDAO extends DAO<Customer>{
}
public class TestCustomerDAO {
public static void main(String[] args) {
CustomerDAO c = new CustomerDAO();
c.add(new Customer());
c.get(0);
}
}
【注意点】
1.对象实例化时不指定泛型,默认为:Object。
2.泛型不同的引用不能相互赋值。
3.加入集合中的对象类型必须与指定的泛型类型一致。
4.静态方法中不能使用类的泛型。
5.如果泛型类是一个接口或抽象类,则不可创建泛型
类的对象。
6.不能在catch中使用泛型
7.从泛型类派生子类,泛型类型需具体化
4.泛型与继承的关系
A类是B类的子类,G是带泛型声明的类或接口。那么G<A>不是G<B>的子类!
5.通配符:?
A类是B类的子类,G是带泛型声明的类或接口。则G<?> 是G<A>、G<B>的父类!
①以List<?>为例,能读取其中的数据。因为不管存储的是什么类型的元素,其一定是Object类的或其子类的。
①以List<?>为例,不可以向其中写入数据。因为没有指明可以存放到其中的元素的类型!唯一例外的是:null
6*. List<? extends A> :可以将List<A>的对象或List<B>的对象赋给List<? extends A>。其中B 是A的子类
? super A:可以将List<A>的对象或List<B>的对象赋给List<? extends A>。其中B 是A的父类
相关文章推荐
- Java集合中使用泛型参数及泛型上下限的问题
- NHibernate 之 “ 值“System.Object[]”不是“”类型,不能在此泛型集合中使用。 参数名: value”
- 第四章 集合与泛型 第二课: 有序列表的使用
- java 基础总结 -- enum 枚举与接口、泛型、集合的使用
- 三层架构中使用实体类填充泛型集合代替DataTable解决方案(ASP.NET+C#)
- 泛型集合的使用
- 使用了泛型的集合 进行存储 、遍历
- NET C# List泛型集合 使用方法
- 三层架构中使用实体类填充泛型集合代替DataTable解决方案(ASP.NET+C#)
- java语言基础(68)——集合框架(泛型概述和使用)
- 牛客网Java刷题知识点之泛型概念的提出、什么是泛型、泛型在集合中的应用、泛型类、泛型方法、泛型接口、泛型限定上限、泛型限定下限、 什么时候使用上限?泛型限定通配符的体现
- 三层架构中使用实体类填充泛型集合代替DataTable解决方案(ASP.NET+C#)
- 三层架构中使用实体类填充泛型集合代替DataTable解决方案(ASP.NET+C#)
- 使用泛型集合取代datatable作为返回值实现面向对象
- NET C# List泛型集合 使用方法
- 泛型(一)集合中使用泛型
- 泛型集合类型,赋予集合业务意义,增强集合的抽象使用
- 【C#】之使用泛型集合管理对象
- C#中关于在List集合类中使用泛型的例子
- 使用泛型集合简化数据查询