关于Object[]一直找不到原因的疑问?

QuinnNorris 2017-01-17 10:06:58
记得好多次涉及到Object[]的代码都出现了这样的问题,然而百度了一下并不能解决,求个大神来指点迷津。。。。

StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
formatter.format("%4$2s %3$2s %2$2s %1$2s","a", "b", "c", "d");

第三行的format红色下划线:The method format(Locale, String, Object[]) in the type Formatter is not applicable for the arguments (String, String, String, String, String)

代码的道理我能理解,但是不知道为什么会报错,代码写的应该没错吧, MyEclipse中的compiler也调为1.7,但是没用。基本上遇到了Object[]的代码全都有这个问题。

除非这样写 才不报错:
Object[] obj = {"a", "b", "c", "d"};
formatter.format("%4$2s %3$2s %2$2s %1$2s", obj);
但是这样写太笨重了,想问问第一种情况能不能改正过来。
...全文
290 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
QuinnNorris 2017-01-22
  • 打赏
  • 举报
回复
引用 6 楼 bcsflilong 的回复:
[quote=引用 4 楼 QuinnNorris 的回复:] [quote=引用 3 楼 bcsflilong 的回复:] 你导错包了 应该是 java.util.Formatter 而不是 java.util.logging.Formatter
包导的是java.util.Formatter[/quote]在我电脑上的eclipse中 导入java.util.Formatter不报错 导入java.util.logging.Formatter 提示The method format(Locale, String, Object[]) in the type Formatter is not applicable for the arguments (String, String, String, String, String) [/quote] 嗯 看来很有可能是我的myeclipse 的问题
cc_1993 2017-01-20
  • 打赏
  • 举报
回复
formatter.format("%4$2s %3$2s %2$2s %1$2s",new Object{"a", "b", "c", "d"});
bcsflilong 2017-01-19
  • 打赏
  • 举报
回复
引用 4 楼 QuinnNorris 的回复:
[quote=引用 3 楼 bcsflilong 的回复:] 你导错包了 应该是 java.util.Formatter 而不是 java.util.logging.Formatter
包导的是java.util.Formatter[/quote]在我电脑上的eclipse中 导入java.util.Formatter不报错 导入java.util.logging.Formatter 提示The method format(Locale, String, Object[]) in the type Formatter is not applicable for the arguments (String, String, String, String, String)
QuinnNorris 2017-01-19
  • 打赏
  • 举报
回复
引用 1 楼 xiesisi3 的回复:
formatter.format("%4$2s %3$2s %2$2s %1$2s",{"a", "b", "c", "d"});
这个方法好像不行
QuinnNorris 2017-01-19
  • 打赏
  • 举报
回复
引用 3 楼 bcsflilong 的回复:
你导错包了 应该是 java.util.Formatter 而不是 java.util.logging.Formatter
包导的是java.util.Formatter
bcsflilong 2017-01-18
  • 打赏
  • 举报
回复
你导错包了 应该是 java.util.Formatter 而不是 java.util.logging.Formatter
moguobiao 2017-01-18
  • 打赏
  • 举报
回复
在我的IDE没有红色下划线。请把完整的原程式post出来。
xiesisi3 2017-01-18
  • 打赏
  • 举报
回复
formatter.format("%4$2s %3$2s %2$2s %1$2s",{"a", "b", "c", "d"});

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧