我试图在我的项目中使用对象数组,但出现错误:
incompatible types: Object cannot be converted to String
在这条线上:
ST1 = new String[]{emt1, emt2, emt3, emt4};
现在我不知道此错误的原因是什么。请帮我 。
Object[] ST1; Object emt1,emt2,emt3,emt4; private void jButton3ActionPerformed(ActionEvent evt) { try { emt1 = null; emt2 = null; emt3 = null; emt4 = null; ST1 = new String[]{emt1, emt2, emt3, emt4}; } ....
您有两种方法,一种是将每个对象emt1,emt2,..强制转换为String,如下所示:
ST1 = new String[]{(String)emt1, (String)emt2, (String)emt3, (String)emt4};
或者,您应该更改属性的类型:
Object emt1, emt2, emt3, emt4;
到弦
String emt1, emt2, emt3, emt4; ST1 = new String[]{emt1, emt2, emt3, emt4};