java - 有人可以帮助将这个简洁的 Java 函数解构为简单的英语吗?

标签 java xojo

我正在尝试移植 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;
}
变量 queueObject[]类上定义的数组。
有几行让我感到困惑。首先: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 decrement size, assign that value to n and then access this index within the es array?


是的。

If so, what does x = (E) before this expression mean? I'm guessing it means to cast the element to type E?


是的,再次只是像以前一样的 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是的,那没关系,NumberInteger的父类(super class)这已经足够了。

关于java - 有人可以帮助将这个简洁的 Java 函数解构为简单的英语吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63908868/

相关文章:

java - 在 CircleCi 中从 Spring Boot 访问 PostgreSQL 9.6

oauth-2.0 - 使用 Xojo 和 Chilkat 与 Xero 进行 OAuth2 集成

java - android xml属性android :onClick ="..." work behind the scenes?怎么办

java - 用java连接微软Exchange服务器

json - 如何使用XOJO从Web加载JSON?

macos - REALBasic 值 50 美元吗?

带有重音符号的 Android 和 MSSQL 服务器排序规则

java - 如何在java中声明和调用包含指针的native方法

java - 登录后Android存储用户ID