泛型的引入和体现:

问题:集合中能够存储各种类型的元素,可是由于集合能够存储各种类型数据。在获取集合中元素时,就会造成数据不安全。

public class GenericDemo {

	public static void main(String []str)
{
List list = new ArrayList();
list.add(10);//自己主动装箱
list.add(new Integer(100));
list.add(Integer.valueOf(1000)); for(Object obj:list)
System.out.println(obj);
} }

在开发中,能够会发生一种情况:用户输入了一个数据并存储到集合中。可是用户可能输入String类型的数据

public static void main(String []str)
{
List list = new ArrayList();
list.add("10");
list.add(new Integer(100));
list.add(Integer.valueOf(1000)); for(Object obj:list)
{
Integer num = (Integer)obj;
System.out.println(num);
} }

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

    at cn.itcast.test.GenericDemo.main(GenericDemo.java:17)

解决方式:能够使用instanceofkeyword避免ClassCastException异常

在JDK1.5后。出现了一种新的解决方式:泛型

思考:假设集合中仅仅能存储一个类型的元素,还会出现获取数据时不安全的问题吗?

 实现方式:在定义集合时明白了该集合中能够存储元素的类型

泛型的使用格式:   <引用类型>    注意:泛型中仅仅能使用引用类型

package org.test;

import java.util.ArrayList;
import java.util.List; public class TestGeneric {
public static void main(String[] args) {
//自己定义泛型类的使用
//1.实例化泛型类的对象时指明泛型的类型,这儿不能一般类型,仅仅能是引用类型
//全部使用了泛型类型的地方。都要变成泛型类的类型
//2.假设不指明类型,默认是Object类型
order<Integer> o = new order<Integer>(); //order oo = new order();//假设不指定泛型的类型,默认是Object类型 List<Integer>list = o.getList();
list.add(10);
list.add(11); System.out.println(o.getList()); Integer i = o.getE(100);
System.out.println(i);
} } class order<T>
{
int MaxSize = 30;
//public T []t = new T[MaxSize]; 没有泛型数组
private T t;
private List<T> list = new ArrayList<T>(); public List<T> getList()
{
return list;
}
//声明泛型方法 <E>一定要在权限修饰符后面。不能缺少
public <E> E getE(E e)
{
return e;
} }
//子类还是不确定类型T ,实例化子类的对象时候要指明泛型的引用类型 //继承泛型类或泛型接口时,能够指明泛型的类型 class SubOrder<Integer> extends order<Integer>
class SubOrder <T>extends order<T>
{ }

泛型的优点?

1,  攻克了集合中存储数据的不安全性

2,  把执行时可能发生的异常,放在编译时作为编译错误处理了,避免了执行时的异常

3。  省略了代码中的强制类型转换的书写

@Test
public void Demo2()
{
List<Integer> list = new ArrayList<Integer>();
list.add(10);
list.add(new Integer(100));
list.add(Integer.valueOf(1000)); //使用迭代器遍历集合。在迭代器中明白要迭代的类型
for (Iterator<Integer> it = list.iterator(); it.hasNext();) {
//由于迭代器明白了要迭代的类型。所以不须要强制类型转换
//Integer num = (Integer)it.next();
//不须要强制类型转换
Integer num = it.next();
System.out.println(num);
}
//使用foreach遍历集合
//由于list集合中明白了集合的类型,所以在foreach中能够直接声明为集合中的元素类型,而不是之前的Object
for(Integer i:list)
{
System.out.println(i);
} }

最新文章

  1. SSH远程登录原理与运用
  2. Event List
  3. sencha gridpanel改变单元格颜色
  4. 【转载】C内存分配
  5. 在Hyper-V的虚拟机中使用无线网络
  6. Poj(2135),MCMF,模板
  7. WinSock编程基础
  8. R语言语法笔记
  9. BZOJ 1878 SDOI 2009 HH项链 树状数组 + 脱机处理
  10. 【6】Laravel5.1的migration数据库迁移
  11. VC调试篇
  12. centos 7.1搭建docker本地私有仓库返回500错误
  13. 手把手教你怎么搭建angular+gulp的项目(一)
  14. 常用的Character方法
  15. 解决NO migrations to apply
  16. nginx 学习笔记(3) nginx管理
  17. Terrain tessellation &amp;&amp;Threaded Rendering Vk
  18. eclipse逆向生成实体类注解方式或者xml方式
  19. php自带的filter过滤函数
  20. Flume实例一学习

热门文章

  1. D. Dasha and Chess(交互题)
  2. 【原创】Linux环境下的图形系统和AMD R600显卡编程(2)——Framebuffer、DRM、EXA和Mesa简介【转】
  3. 【译】在Asp.Net Core 中使用外部登陆(google、微博...)
  4. JMeter出现“the target server failed to respond“的解决办法
  5. spring-web.xml 模板
  6. 使用JS实现俄罗斯方块游戏
  7. tomcat启动不成功(点击startup.bat闪退)的解决办法
  8. SQL Server 4
  9. Angular 快速学习笔记(1) -- 官方示例要点
  10. P2817 宋荣子的城堡