Java 7 上 JCombobox 的类型安全
我无法摆脱 java7 中的一种新的类型安全警告。
我定义了以下 JCombobox 对象
private JComboBox<Integer> combobox_current_year;
,并且构造函数
combobox_current_year = new JComboBox(options.getList_years().toArray());
Java 7 现在给出了以下警告:
类型安全:JComboBox 类型的表达式需要取消选中 转换以符合 JComboBox
将代码更改为后,
combobox_current_year = new JComboBox<Integer>((Integer[]) options.getList_years().toArray());
出现以下异常:
线程“AWT-EventQueue-0”中出现异常 java.lang.ClassCastException: [Ljava.lang.Object; 无法转换为 [Ljava.lang.Integer;在 jamm.gui.FinanzmanagerGui.mainWindow(FinanzmanagerGui.java:123) 在 jamm.StartJamm$1.run(StartJamm.java:43) 在 java.awt.event.InitationEvent.dispatch(InitationEvent.java:251) 在 java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705) 在 java.awt.EventQueue.access$000(EventQueue.java:101) 在 java.awt.EventQueue$3.run(EventQueue.java:666) 在 java.awt.EventQueue$3.run(EventQueue.java:664) 在 java.security.AccessController.doPrivileged(本机方法)位于 java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.awt.EventQueue.dispatchEvent(EventQueue.java:675) 处 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
I cant get rid of one new typesafety warning in java7.
I have the following JCombobox object defined
private JComboBox<Integer> combobox_current_year;
And the the constructor
combobox_current_year = new JComboBox(options.getList_years().toArray());
Java 7 gives me now the following warning:
Type safety: The expression of type JComboBox needs unchecked
conversion to conform to JComboBox
After changing the code to
combobox_current_year = new JComboBox<Integer>((Integer[]) options.getList_years().toArray());
I get the following exception:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.Object;
cannot be cast to [Ljava.lang.Integer; at
jamm.gui.FinanzmanagerGui.mainWindow(FinanzmanagerGui.java:123) at
jamm.StartJamm$1.run(StartJamm.java:43) at
java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705) at
java.awt.EventQueue.access$000(EventQueue.java:101) at
java.awt.EventQueue$3.run(EventQueue.java:666) at
java.awt.EventQueue$3.run(EventQueue.java:664) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675) at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于
toArray()
方法返回一个Object[]
类型的对象,该对象与Integer[]
类型完全不同(因此导致演员失败)。您必须将不同类型的数组传递给 toArray(...) 才能解决该问题。从列表中获取数组的最有效方法是:我使用临时变量来保存列表,因为我想预先调整数组的大小,以便可以将值复制到其中。传入较短的数组(例如,长度为 0)也可以,但会导致发生额外的分配。 (您可以通过将长度为零的数组保留在
private static
字段中来使其工作;它实际上是不可变的,因此您可以共享它而不会产生不良后果。但我更喜欢预先调整大小,因为这样就可以减少班级层面的混乱。)The problem is that the
toArray()
method returns an object of typeObject[]
which is a totally different type toInteger[]
(so making the cast fail). You have to pass in a differently-typed array totoArray(…)
to resolve that. The most efficient method of getting an array out of a list is this:I use a temporary variable to hold the list because I want to pre-size the array so that the values can be just copied into it. Passing in a shorter array (e.g., of length 0) would also work, but would cause an extra allocation to happen. (You could make it work by keeping the length-zero array in a
private static
field; it's effectively immutable so you can share it with no ill-consequences. But I prefer to pre-size as that leaves less clutter at the class level.)我猜这意味着
options.getList_years()
返回一个List
。这意味着您需要从 Object[] 转换为 Integer[],这必须通过复制操作来完成。
I guess that means that
options.getList_years()
returns aList<?>
.This means you need to convert from an Object[] to an Integer[], which has to be done with a copy operation.
尝试执行
集合框架的 toArray 总是返回一个 Object[] 如果你想要一个特定的数组类型,你需要提供一个
try doing
toArray of the collection framework always returns a Object[] if you want a specific array type you need to supply one