Java 7 上 JCombobox 的类型安全

发布于 2024-12-14 09:58:12 字数 1759 浏览 3 评论 0原文

我无法摆脱 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∞觅青森が 2024-12-21 09:58:13

问题在于 toArray() 方法返回一个 Object[] 类型的对象,该对象与 Integer[] 类型完全不同(因此导致演员失败)。您必须将不同类型的数组传递给 toArray(...) 才能解决该问题。从列表中获取数组的最有效方法是:

List<Integer> tmp = options.getList_years();
combobox_current_year = new JComboBox<Integer>(
        tmp.toArray(new Integer[tmp.size()]));

我使用临时变量来保存列表,因为我想预先调整数组的大小,以便可以将值复制到其中。传入较短的数组(例如,长度为 0)也可以,但会导致发生额外的分配。 (您可以通过将长度为零的数组保留在 private static 字段中来使其工作;它实际上是不可变的,因此您可以共享它而不会产生不良后果。但我更喜欢预先调整大小,因为这样就可以减少班级层面的混乱。)

The problem is that the toArray() method returns an object of type Object[] which is a totally different type to Integer[] (so making the cast fail). You have to pass in a differently-typed array to toArray(…) to resolve that. The most efficient method of getting an array out of a list is this:

List<Integer> tmp = options.getList_years();
combobox_current_year = new JComboBox<Integer>(
        tmp.toArray(new Integer[tmp.size()]));

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.)

伪装你 2024-12-21 09:58:13

我猜这意味着 options.getList_years() 返回一个 List

这意味着您需要从 Object[] 转换为 Integer[],这必须通过复制操作来完成。

Object[] years = options.getList_years().toArray();
new JComboBox(Arrays.copyOf(years, years.length, Integer.class));

I guess that means that options.getList_years() returns a List<?>.

This means you need to convert from an Object[] to an Integer[], which has to be done with a copy operation.

Object[] years = options.getList_years().toArray();
new JComboBox(Arrays.copyOf(years, years.length, Integer.class));
温馨耳语 2024-12-21 09:58:12

尝试执行

combobox_current_year = new JComboBox<Integer>(options.getList_years().toArray(new Integer[0])); 

集合框架的 toArray 总是返回一个 Object[] 如果你想要一个特定的数组类型,你需要提供一个

try doing

combobox_current_year = new JComboBox<Integer>(options.getList_years().toArray(new Integer[0])); 

toArray of the collection framework always returns a Object[] if you want a specific array type you need to supply one

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文