由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教一个java的comparator的问题
相关主题
请教C++ STL中priority_queue模板参数中的Compare函数server端用Threadpool实现request/response的两种不同方法比较
请教一个系统设计问题how to know the contents in Message queue?
问个很弱的stl的priority queue问题How does YAHOO calculate RSI? (转载)
STL里的priority_queue到底有啥用?请教template和factory有啥区别?
mutex和semaphore的差别到底是什么?map是用什么data structure来implement的?
问一个private destructor的问题再来讨论一直算法课的作业吧
一个multithread的问题(是不是有人觉的很简单?)MINOS linear programming
请教java中compareTo的方法有人知道免费的min cost network flow solver么? (转载)
相关话题的讨论汇总
话题: comparator话题: minpq话题: searchnode话题: key话题: private
进入Programming版参与讨论
1 (共1页)
a*******y
发帖数: 199
1
最近在学点java,遇到个问题想请教。
首先有一个类的定义如下:
public class MinPQ implements Iterable {
private Key[] pq; // store items at indices 1 to N
private int N; // number of items on priority
queue
private Comparator comparator; // optional comparator
/**
* Create an empty priority queue with the given initial capacity.
*/
public MinPQ(int initCapacity) {
pq = (Key[]) new Object[initCapacity + 1];
N = 0;
}
/**
* Create an empty priority queue.
*/
public MinPQ() { this(1); }
/**
* Create an empty priority queue with the given initial capacity,
* using the given comparator.
*/
public MinPQ(int initCapacity, Comparator comparator) {
this.comparator = comparator;
pq = (Key[]) new Object[initCapacity + 1];
N = 0;
}
/**
* Create an empty priority queue using the given comparator.
*/
public MinPQ(Comparator comparator) { this(1, comparator); }
我现在想在另一个类中用最后一个构造函数来定义一个变量,代码如下:
public class Solver {
private Searchnode lpsrchnode;
private Searchnode twin_lpsrchnode;
private boolean issolvable;
private int moves;
private int twin_moves;
class Searchnode {
Searchnode prenode;
Board bd;
int moves;
}
public static class SrcnodeComparator implements Comparator {
public int compare(Board bd1, Board bd2) {
if (bd1.manhattan() > bd2.manhattan())
return 1;
else if (bd1.manhattan() < bd2.manhattan())
return -1;
else
return 0;
}
}
private MinPQ pq = new MinPQ(
new SrcnodeComparator());
private MinPQ twin_pq = new MinPQ(
new SrcnodeComparator());
但上述两个变量pq和twin_pq定义一直有错,不知道为什么。正确的写法又应该是怎么
样的呢?多谢啦!
z****e
发帖数: 54598
2
eclipse跟你说是什么问题?
W***o
发帖数: 6519
3
my 2c: the problem is with your static comparator class, because eclipse
cannot see the return value - as it is buried in the if-else blocks. I would
suggest you put the last "return 0" as a default (outside the if-else) for
the comparator method.
public int compare(Board bd1, Board bd2) {
if (bd1.manhattan() > bd2.manhattan())
return 1;
else if (bd1.manhattan() < bd2.manhattan())
return -1;
return 0;
}

【在 a*******y 的大作中提到】
: 最近在学点java,遇到个问题想请教。
: 首先有一个类的定义如下:
: public class MinPQ implements Iterable {
: private Key[] pq; // store items at indices 1 to N
: private int N; // number of items on priority
: queue
: private Comparator comparator; // optional comparator
: /**
: * Create an empty priority queue with the given initial capacity.
: */

1 (共1页)
进入Programming版参与讨论
相关主题
有人知道免费的min cost network flow solver么? (转载)mutex和semaphore的差别到底是什么?
matlab C API 怎么用啊??问一个private destructor的问题
控制程序自动化执行, 该用 perl, python or shell script ?一个multithread的问题(是不是有人觉的很简单?)
用C++的写的numerical or optimization solver library请教java中compareTo的方法
请教C++ STL中priority_queue模板参数中的Compare函数server端用Threadpool实现request/response的两种不同方法比较
请教一个系统设计问题how to know the contents in Message queue?
问个很弱的stl的priority queue问题How does YAHOO calculate RSI? (转载)
STL里的priority_queue到底有啥用?请教template和factory有啥区别?
相关话题的讨论汇总
话题: comparator话题: minpq话题: searchnode话题: key话题: private