在Matlab中使用Java PriorityQueue [英] Using Java PriorityQueue in Matlab

查看:171
本文介绍了在Matlab中使用Java PriorityQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab中需要最小化堆,并且正在尝试使用Java的PriorityQueue.我被困在如何提供比较器上. 到目前为止,我已经初始化了PriorityQueue并可以向其中添加一个值-索引对:

I need a min-heap in Matlab and I'm trying to use Java's PriorityQueue. I'm stuck on how to supply the Comparator. So far I have initialized the PriorityQueue and can add one value-index pair to it:

>> q = java.util.PriorityQueue
q =
[]
>> q.add({1,3})
ans =
     1

当我尝试添加更多数据时出现问题:

The problem occurs when I try to add more data:

>> q.add({2,4})
??? Java exception occurred: 
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.lang.Comparable
    at java.util.PriorityQueue.siftUpComparable(Unknown Source)
    at java.util.PriorityQueue.siftUp(Unknown Source)
    at java.util.PriorityQueue.offer(Unknown Source)
    at java.util.PriorityQueue.add(Unknown Source)

帖子中,我看到我需要提供比较器功能,但我不知道该怎么做.

From this post, I see that I need to supply a Comparator function, but I don't know how to do this.

推荐答案

优先级队列要么包含实现Comparable的对象,要么需要在构造时传递Comparator函数.

Priority queues need to either contain objects that implement Comparable, or you need to pass in a Comparator function at construction time.

MATLAB当前没有一种方法可以用MATLAB代码实现Java接口,也不能提供文字Java代码.

There isn't a way currently in MATLAB to either implement Java interfaces with MATLAB code, or supply literal Java code.

因此,您必须遵循@nibot的建议,并制作一个小的.jar文件,其中包含实现Comparator的类.

So you'd have to follow @nibot's suggestion and make a small .jar file containing a class implementing Comparator.

这篇关于在Matlab中使用Java PriorityQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆