用作 StringBuilder 的 deleteCharAt() 方法参数的双精度值

Double value used as argument for the StringBuilder's deleteCharAt() method

我正在准备 Oracle Associate 认证并正在阅读以下书籍:

Oracle Certified Associate,Java SE7 程序员学习指南

在第 77 页,有一个知识测试,问题如下:

Given the following declaration :

StringBuilder sb = new StringBuilder();

Which of the following are valid uses of the sb variable?

a. sb.append(34.5); b. sb.deleteCharAt(34.5); c. sb.toInteger(3); d. sb.toString();

我假设正确答案是 a & d。

但这是书中的答案:

a, b, and d There is no StringBuilder toInteger method.

没有其他解释。

所以,我去查看了源代码,这里是:

279    public StringBuilder deleteCharAt(int index) {
280        super.deleteCharAt(index);
281        return this;
282    }

int 作为参数并且该方法不会在其他任何地方重载。

你知道书中对此"correction"的解释吗?

对于上面给出的例子sb.deleteCharAt(34.5)

编译器说

The method deleteCharAt(int) in the type StringBuilder is not applicable for the arguments (double)

那么书上的答案是错误的

书中有误。你的答案是正确的,应该只有a&d。这里有一个link, https://www.packtpub.com/books/content/support/10143 并找到以下勘误表标题(大约在页面中间): 勘误表类型:技术 |页码:297 |勘误日期:2013 年 2 月 5 日

老实说,这看起来像是书中的错误,您可能需要联系出版商。

虽然您可以将 34.5 转换为 int,但会丢失精度,这不会发生在答案 B 中。