instanceof Double/Object 不起作用:(
我遇到了java的instanceof问题。这是给我带来麻烦的代码间隙:
LinkedList<Double> currentSummary = summary.getFirst().getQuantiles();
...更多代码...
while (!currentSummary.isEmpty()){
if (currentSummary.getFirst() instanceof Double){
orderedSummary.add(new ComparableWrapper<Double, Float>(currentSummary.removeFirst(), currentEpsilon));
}
}
所以,我的问题是,if 条件不会变为真。 currentSummary
中的这些元素是 null
或 Double
值。我试图拒绝 null
的元素。起初,我只是添加了它们,但后来遇到了 NullPointerException,因为一些(但不是全部!)元素为 null。 currentSummary的示例元素是例如[null, 0.09861866469135272, 10.137051035535745, 107.12083740100329, 371.4371264801424, 827.432799544501, 1206。 251577083686]。
有人知道为什么 instanceof
在这种情况下不起作用吗?我也用 currentSummary.getFirst() instanceof Object
尝试过......
提前致谢!
I've got a problem with java's instanceof. Here's a gap of code that causes me trouble:
LinkedList<Double> currentSummary = summary.getFirst().getQuantiles();
...more code...
while (!currentSummary.isEmpty()){
if (currentSummary.getFirst() instanceof Double){
orderedSummary.add(new ComparableWrapper<Double, Float>(currentSummary.removeFirst(), currentEpsilon));
}
}
So, my problem is, that the if-condition won't become true. Those elements in currentSummary
are either null
or an Double
-value. And I'm trying to reject elements that are null
. At first I just added them and ran into NullPointerException
later on, because of some (but not all!) elements being null
.
An example element of currentSummary
is e.g. [null, 0.09861866469135272, 10.137051035535745, 107.12083740100329, 371.4371264801424, 827.432799544501, 1206.251577083686].
Anybody got's an idea why instanceof
won't work in that case? I tried it with currentSummary.getFirst() instanceof Object
as well...
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您想在每次迭代时删除第一个条目,以便遍历完整列表。但是,仅当 instanceof 条件为 true 时才删除该条目。因此,当遇到第一个空值时,循环似乎变得无限(除非您删除了部分代码,并且我们看不到完整的循环体)
I assume you want to remove the first entry at each iteration, in order to traverse the complete list. However, you remove the entry only when the instanceof condition is true. Therefore, it seems like the loop becomes infinite when it encounters the first null value (unless you dropped parts of the code, and we don't see the complete loop body)
是否有可能由于 自动装箱/拆箱,Double 对象被拆箱为原始
double
,因此它们不是Double
类的实例。如果是这种情况,检查非空元素是否会更好?
Is it possible that due to auto-boxing/unboxing, the Double objects get unboxed to the primitive
double
, and therefore they are not instance ofDouble
class.If that is the case, would it be better if you check for non-null elements instead?
为什么不使用简单的空检查呢?
Why don't you just use a simple null check?
通过使用原始类型(
LinkedList
而不是LinkedList
),可以将非Double
条目添加到您的LinkedList
。这些元素将无法通过instanceof Double
测试。但这些条目将通过instanceof Object
测试。It is possible, by using raw types (
LinkedList
instead ofLinkedList<Double>
), to add non-Double
entries to yourLinkedList<Double>
. Those elements would fail theinstanceof Double
test. But those entries would pass aninstanceof Object
test.