我正在尝试移植 PriorityQueue
来自 OpenJDK implementation 的类到另一种没有类似数据结构的语言(Xojo)。我真的很难将以下方法分解为伪代码,以便将其转换为 Xojo:
public E poll() {
final Object[] es;
final E result;
if ((result = (E) ((es = queue)[0])) != null) {
modCount++;
final int n;
final E x = (E) es[(n = --size)];
es[n] = null;
if (n > 0) {
final Comparator<? super E> cmp;
if ((cmp = comparator) == null)
siftDownComparable(0, x, es, n);
else
siftDownUsingComparator(0, x, es, n, cmp);
}
}
return result;
}
变量 queue
是 Object[]
类上定义的数组。有几行让我感到困惑。首先:
if ((result = (E) ((es = queue)[0])) != null)
这是否意味着“将数组 queue
分配给变量 es
和访问元素 0
,如果它是 而不是 null,则执行以下操作?” result = (E)
有什么用表达是什么意思?我知道 E
是泛型类型。final E x = (E) es[(n = --size)];
的操作顺序是什么? ?这是否意味着递减size
,将该值赋给 n
然后在 es
中访问该索引大批?如果是这样,是什么x = (E)
之前这个表达是什么意思?我猜这意味着将元素转换为类型 E
?最后,这些行:
final Comparator<? super E> cmp;
if ((cmp = comparator) == null)
comparator
是一个类变量(持有 Comparator
)。为什么将它分配给局部变量 cmp
第一行的问号是什么意思?
最佳答案
Does this mean "assign the array queue to the variable
es
"
是的。
and access element 0 and do the following if it's not null?
是的。
What does the
result = (E)
expression mean?
上面两个表达式同时赋值
queue[0]
至 result
. (E)
是一个类型的强制转换。所以它基本上只是:result = queue[0]
加上一些额外的东西。
final E x = (E) es[(n = --size)];
? Does this mean decrementsize
, assign that value ton
and then access this index within thees
array?
是的。
If so, what does
x = (E)
before this expression mean? I'm guessing it means to cast the element to typeE
?
是的,再次只是像以前一样的 Actor 阵容。
comparator
is a class variable
只是为了迂腐,
comparator
可能是实例变量,而不是类变量。检查其定义。Why assign it to a local variable
cmp
我想制作一个局部变量副本。我在代码中没有看到这样做的充分理由,所以它可能是一个错误或在某些先前的代码被更改后留下的东西。
and what does the question mark mean on the first line?
问号表示
Comparator
的类型未知,可以是任何东西,只要它是 E
的父类(super class).例如,如果 Integer
没有 Comparator
但是 Number
是的,那没关系,Number
是Integer
的父类(super class)这已经足够了。
关于java - 有人可以帮助将这个简洁的 Java 函数解构为简单的英语吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63908868/