
arraylist的泛型转换机制
arraylist的get()方法可以将object数组中的元素直接返回为泛型类型,这与其他自定义类中需要类型转换形成了鲜明对比。下面解释了背后的机制。
在arraylist的源码中,elementdata是一个object数组,它存储着元素。get()方法直接返回elementdata[index],而无需类型转换。这是因为java泛型实现了类型擦除。
类型擦除
泛型类型在编译时被擦除,也就是说泛型信息在运行时丢失。这意味着get()方法无法在运行时知道object数组中的元素的实际类型。
实际转换
因此,实际的类型转换不是在get()方法中进行的,而是在调用get()方法的位置进行的。例如,如果将arraylist声明为arraylist,则get()方法返回的object对象将被jvm自动转换为string。
Get笔记
Get笔记,一款AI驱动的知识管理产品
125 查看详情
自定义类中的类型转换
在自定义类中,类型转换需要显式指定,因为jvm不知道方法返回的对象的实际类型。这是因为缺少类型擦除,泛型信息在运行时可用。
示例代码
以下代码说明了类型检查在不同场景中的不同发生位置:
import java.lang.reflect.method;import java.lang.reflect.modifier;import java.lang.reflect.parameter;public class generictypeinspection { public static void main(string[] args) { class arraylistclass = arraylist.class; method getmethod = arraylistclass.getmethod("get", int.class); // 获取get方法的参数类型 parameter parameter = getmethod.getparameters()[0]; system.out.println("arraylist get method parameter type: " + parameter.gettype()); // (实际的类型转换发生在这里) getmethod.invoke(new arraylist(), 1); // 获取customgenericclass里的方法 class customgenericclass = customgenericclass.class; method getintegermethod = customgenericclass.getmethod("getinteger"); // (类型转换需要显式指定) integer integer = (integer) getintegermethod.invoke(new customgenericclass()); } private static class customgenericclass { public t getinteger() { return (t) new object(); } }}
运行这段代码,输出结果将如下所示:
arraylist get method parameter type: int
(实际的类型转换发生在这里)
CustomGenericClass里的方法(类型转换需要显式指定)
以上就是ArrayList的get()方法为何无需显式类型转换?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/374347.html
微信扫一扫
支付宝扫一扫