instanceof Double/Object 不起作用:(

发布于 2024-09-08 01:56:57 字数 902 浏览 2 评论 0原文

我遇到了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 中的这些元素是 nullDouble 值。我试图拒绝 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

征﹌骨岁月お 2024-09-15 01:56:57

我假设您想在每次迭代时删除第一个条目,以便遍历完整列表。但是,仅当 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)

羞稚 2024-09-15 01:56:57

是否有可能由于 自动装箱/拆箱,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 of Double class.

If that is the case, would it be better if you check for non-null elements instead?

余罪 2024-09-15 01:56:57

为什么不使用简单的空检查呢?

if (currentSummary.getFirst() != null){ ... }

Why don't you just use a simple null check?

if (currentSummary.getFirst() != null){ ... }
司马昭之心 2024-09-15 01:56:57

通过使用原始类型(LinkedList 而不是 LinkedList),可以将非 Double 条目添加到您的 LinkedList。这些元素将无法通过 instanceof Double 测试。但这些条目将通过 instanceof Object 测试。

It is possible, by using raw types (LinkedList instead of LinkedList<Double>), to add non-Double entries to your LinkedList<Double>. Those elements would fail the instanceof Double test. But those entries would pass an instanceof Object test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文