ArrayList的get()方法为何无需显式类型转换?
技术百科
聖光之護
发布时间:2024-12-01
浏览: 次 arraylist的泛型转换机制
arraylist的get()方法可以将object数组中的元素直接返回为泛型类型,这与其他自定义类中需要类型转换形成了鲜明对比。下面解释了背后的机制。
在arraylist的源码中,elementdata是一个object数组,它存储着元素。get()方法直接返回elementdata[index],而无需类型转换。这是因为java泛型实现了类型擦除。
类型擦除
泛型类型在编译时被擦除,也就是说泛型信息在运行时丢失。这意味着get()方法无法在运行时知道object数组中的元素的实际类型。
实际转换
因此,实际的类型转换不是在get()方法中进行的,而是在调用get()方法的位置进行的。例如,如果将arraylist声明为arraylist
自定义类中的类型转换
在自定义类中,类型转换需要显式指定,因为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里的方法 (类型转换需要显式指定)
# ai
# 是在
# 是一个
# 这段
# 在这里
# 形成了
# 自定义
# 对象
# Java
# String
# 泛型
# 类中
# 类型转换
# Object
# 组中
# 这是因为
# jvm
# 擦除
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- Mac怎么进行语音输入_Mac听写功能设置与使用【
- Windows 11如何查看系统激活密钥_Wind
- 使用类变量定义字符串常量时的类型安全最佳实践
- Win11怎么关闭贴靠布局_Win11禁用窗口最大
- Django 密码修改后会话失效的解决方案
- Win11怎么设置默认邮件客户端 Win11修改M
- Python异步编程高级项目教程_asyncio协
- Win11怎么关闭资讯和兴趣_Windows11任
- Win11怎么开启游戏模式_Windows11优化
- 如何在 Pandas 中按元素交集合并两列字符串
- Windows的便笺功能如何使用?(桌面备忘技巧)
- Python lxml的etree和Element
- MAC如何修改默认应用程序_MAC文件后缀关联设置
- win11 OneDrive怎么彻底关闭 Win1
- Python对象生命周期管理_创建销毁解析【教程】
- php打包exe后无法写入文件_权限问题解决方法【
- Win11时间怎么同步到原子钟 Win11高精度时
- Win11怎么设置多显示器任务栏 Win11扩展任
- Windows10电脑怎么设置虚拟光驱_Win10
- Win11怎么退出高对比度模式_Win11取消反色
- 如何在Golang中实现文件下载_Golang文件
- Go 中 defer 在 goroutine 内部
- Win11怎么更改输入法顺序_Win11调整语言首
- windows 10应用商店区域怎么改_windo
- 如何使用Golang读取日志文件_Golang b
- Win11怎么关闭边缘滑动手势_Windows11
- mac怎么打开终端_MAC终端Terminal使用
- php能跑在stm32上吗_php在stm32微控
- Mac怎么查看活动监视器_理解Mac进程和资源占用
- php打包exe如何加密代码_防反编译保护方法【技
- Windows怎样拦截QQ浏览器广告_Window
- Win11怎样安装剪映专业版_Win11安装剪映教
- VSC怎样在Linux运行PHP_Ubuntu系统
- c++ std::future和std::prom
- Win11开始菜单打不开_修复Windows 11
- Win11怎样激活系统密钥_Win11系统密钥激活
- Windows10如何更改系统字体大小_Win10
- Go 中 defer 语句在 goroutine
- c# 在高并发场景下,委托和接口调用的性能对比
- c++如何判断文件是否存在_c++ filesys
- c++中如何计算坐标系中两点间距离_c++勾股定理
- Mac版Final Cut Pro入门_Mac视频
- php做exe支持多线程吗_并发处理实现方式【详解
- Windows10怎么卸载预装软件_Windows
- 如何使用Golang管理模块版本_Golanggo
- php怎么连接数据库_MySQL数据库连接的基础代
- Windows音频驱动无声音原因解析_声卡驱动错误
- Win11关机界面怎么改_Win11自定义关机画面
- Win11怎么清理C盘系统日志_Win11清理系统
- Bpmn 2.0的XML文件怎么画流程图

QQ客服