java unchecked,Java警告:[unchecked]未经检查的转换

本文解析了如何在Java中遇到的关于从raw类型Graph获取ArrayList edges时的编译警告,指出问题在于使用raw类型而非参数化类型,并提供了使用参数化类型或通配符类型的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have the following (partial) class:

public class Graph {

private ArrayList edges;

public ArrayList getEdges() {

return edges;

}

}

Now, when calling the method getEdges() somewhere else and storing the result in a variable of type ArrayList, I get warning: [unchecked] unchecked conversion:

OtherFile.java:101: warning: [unchecked] unchecked conversion

ArrayList edges = graph.getEdges();

^

required: ArrayList

found: ArrayList

I have looked at multiple other questions about this warning, but I can't figure out what I'm doing wrong. getEdges() returns ArrayList, so why can't I store its result in a variable of that exact type?

解决方案

That warning would come when you invoke the getEdges() method on raw type Graph. When that is the case, all the generic types are replaced with their erasure. So, for Graph raw type, the method signature becomes like:

public ArrayList getEdges();

Solution is to use parameterized type or wildcard types.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值