简体   繁体   English

具有通配符的通用数组导致错误

[英]generic array with wildcard cause error

i wanted to create JComboBox´s with Type String, but having it in an array, so i used wildcard for the array and String for the single elements: 我想用String类型创建JComboBox´s,但是将它放在数组中,所以我对数组使用通配符,对单个元素使用String:

JComboBox<?>[] combos=new JComboBox<?>[10];
...
combos[i]=new JComboBox<String>();

same with DefaultComboBoxModels: 与DefaultComboBoxModels相同:

DefaultComboBoxModel<?>[] comboModels=new DefaultComboBoxModel<?>[10];
...
comboModels[i]=new DefaultComboBoxModel<String>();

assigning them to each other: 将它们彼此分配:

...
combos[i].setModel(comboModels[i]);

now gives compiler error (capture#13-of ? not applicable to capture#14-of ?). 现在会给出编译器错误(捕获#13-of不适用于捕获#14-of)。 would have expected that the single elements are set to String and everything´s fine. 会期望将单个元素设置为String,一切都很好。 what´s that about, how to deal with it? 这是怎么回事,怎么处理? (except of doing it raw) (除了原始处理)

Java needs to ensure, that model and combo are of the same type. Java需要确保模型和组合的类型相同。 On the given example, once types are gone after erasure, there is no way to do this. 在给定的示例中,一旦擦除后类型消失,就无法执行此操作。 You could as well set model<Integer> to combo<String> with such way. 您也可以通过这种方式将model<Integer>设置为combo<String>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM