- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 SSCE:
public class ComparableItem implements Comparable<ComparableItem> {
private final int itemNo;
public ComparableItem(final int itemNo) {
this.itemNo = itemNo;
}
@Override
public int compareTo(ComparableItem o) {
if (this.itemNo < o.itemNo) {
return -1;
} else if (this.itemNo > o.itemNo) {
return 1;
}
return 0;
}
}
这是一个演示该问题的测试。创建 PQ 后代码就会中断。上面几行是为了证明由compareTo(..)定义的自然排序按预期工作。打印 PQ 的元素:14,9,15,5,6,3 与预期的 15,14,9,6,5,3。有人可以向我解释一下为什么会出现这种情况吗?
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.*;
public class CompTest{
@Test
public void aTest() {
Integer[] order = {9, 3, 14, 15, 6, 5};
List<ComparableItem> items = new ArrayList<ComparableItem>(order.length);
for (int i = 0; i < order.length; i++) {
items.add(new ComparableItem(order[i]));
}
List<ComparableItem> greater = new ArrayList<ComparableItem>();
testCompare(items, items.get(3), greater, true);
testCompare(items, items.get(2), greater, true);
testCompare(items, items.get(0), greater, true);
testCompare(items, items.get(4), greater, true);
testCompare(items, items.get(5), greater, true);
testCompare(items, items.get(1), greater, true);
final PriorityQueue<ComparableItem> itemsQueue = new PriorityQueue<ComparableItem>(items);
greater = new ArrayList<ComparableItem>();
for (ComparableItem c : itemsQueue) {
testCompare(itemsQueue, c, greater, false);
}
}
public static void testCompare(final Collection<ComparableItem> items, final ComparableItem item, final List<ComparableItem> greater, boolean bigger) {
final int exp = (bigger)? -1:1;
for (ComparableItem c : items) {
final int expected = c.equals(item) ? 0 : greater.contains(c) ? exp : exp*-1;
assertEquals(expected, item.compareTo(c));
assertEquals(expected * -1, c.compareTo(item));
}
greater.add(item);
}
}
new PriorityQueue<ComparableItem>(items);
Creates a PriorityQueue containing the elements in the specified collection. If the specified collection is an instance of a SortedSet or is another PriorityQueue, this priority queue will be ordered according to the same ordering. Otherwise, this priority queue will be ordered according to the natural ordering of its elements.
最佳答案
我认为问题不在于队列,而在于您测试它的方式。特别是 PriorityQueue.iterator()
的 javadoc状态:
Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.
类 javadoc 是这样说的:
The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).
关于java - 如何创建根据列表的自然顺序排序的 PQ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4263393/
我有一套使用两种语言的文档:英语和德语。关于这些文档没有可用的元信息,程序只能查看其内容。基于此,程序必须决定用哪种语言编写文档。 是否有可以在几个小时内实现的针对该问题的“标准”算法?或者,一个免费
背景 我有一个日志系统,可以将记录输出到 std::ostream .每条记录都用一个计数器进行注释,该计数器随着每个输出而增加 1,如下所示: ===== Batch # 5 ===== T
用户可能希望根据需要分隔数字。 从字符串中提取所有(自然)数字的最有效(或简单的标准函数)是什么? 最佳答案 您可以使用正则表达式。我从 Sun's regex matcher tutorial 修改
我认为如果表有代理键而没有(自然)替代键是没有意义的(请记住,代理键的属性之一是它在数据库之外没有意义环境)。 例如假设我有下表: 假设 employee_id 是代理主键,表中没有(自然)备用键。
我想将屏幕方向锁定为其默认方向。我在实现这一点时遇到问题。最初我将屏幕锁定为 list 中的肖像。它适用于纵向默认设备。但是许多平板电脑默认为横向,因此在这些设备中锁定纵向是不合适的,我想检测此默认方
我已将笔记本电脑上的触摸板滚动设置为倒置(自然)。它适用于任何地方(pdf、浏览器等),但在 vscode 中,它坚持正常滚动。通过 vscode 的设置文件没有显示适当的条目。 系统:Ubuntu
在我发现的许多在上限集合上使用可尾游标的示例中,代码包括: hint( { $natural: 1 } ) (例如 here ),包括官方文档 ( here ),以“确保我们不使用任何索引”,并且结果
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: T
一些上下文:Node.js、Bot、natural module . 我想构建一个机器人,并且我正在使用自然模块来解析用户输入并对其进行总体分类。 var classifier = new natur
我是一名优秀的程序员,十分优秀!