无法将原语分配给 Eclipse 中的对象数组
Unable to assign primitives to Object array in Eclipse
我正在初始化对象数组如下:
Object a[] = new Object[4];
a[0] = 1; //while assigning integer value, am getting an error: "Type mismatch: Cannot convert Integer to Object
a[1] = 'A'; //while assigning char value, am getting an error: "Type mismatch: Cannot convert char to Object
a[2] = 12.33//while assigning integer value, am getting an error: "Type mismatch: Cannot convert double to Object
a[3] = "Hello"; //Accepting only string values.
请指出哪里出了问题?是配置问题吗?我正在使用:
Java Version jdk1.8.0_151,
jre1.8.0_151,
Eclipse Java EE IDE for Web Developers.
Version: Oxygen.2 Release (4.7.2)
Build id: 20171218-0600
int
、float
、double
和 char
都是 primitive
类型,因此它们不能 "converted" as [=16] =].
String
是Object
,所以可以在数组中添加。
要实现您可能想要的效果,您可以将它们转换为等效的对象:
Object a[] = new Object[4];
a[0] = Integer.valueOf(1);
a[1] = Character.valueOf('A');
a[2] = Float.valueOf(12.33);
a[3] = "Hello";
我没有遇到你描述的问题。我觉得是自动装箱,至少在我的配置环境下是这样。
public static void main(String[] args) {
// TODO Auto-generated method stub
Object a[] = new Object[4];
a[0] = 1; //while assigning integer value, am getting an error: "Type mismatch: Cannot convert Integer to Object
a[1] = 'A'; //while assigning char value, am getting an error: "Type mismatch: Cannot convert char to Object
a[2] = 12.33;//while assigning integer value, am getting an error: "Type mismatch: Cannot convert double to Object
a[3] = "Hello"; //Accepting only string values.
for(Object i:a) {
System.out.println(i.toString());
}
}
右键单击 -> 运行 作为 -> Java 应用程序,
控制台输出:
1
A
12.33
Hello
如果您使用低于 5 的编译器合规级别,这正是您会收到的错误消息。请注意,这与您系统上的 Java 版本不同。请在此处查看修复 .
我正在初始化对象数组如下:
Object a[] = new Object[4];
a[0] = 1; //while assigning integer value, am getting an error: "Type mismatch: Cannot convert Integer to Object
a[1] = 'A'; //while assigning char value, am getting an error: "Type mismatch: Cannot convert char to Object
a[2] = 12.33//while assigning integer value, am getting an error: "Type mismatch: Cannot convert double to Object
a[3] = "Hello"; //Accepting only string values.
请指出哪里出了问题?是配置问题吗?我正在使用:
Java Version jdk1.8.0_151,
jre1.8.0_151,
Eclipse Java EE IDE for Web Developers.
Version: Oxygen.2 Release (4.7.2)
Build id: 20171218-0600
int
、float
、double
和 char
都是 primitive
类型,因此它们不能 "converted" as [=16] =].
String
是Object
,所以可以在数组中添加。
要实现您可能想要的效果,您可以将它们转换为等效的对象:
Object a[] = new Object[4];
a[0] = Integer.valueOf(1);
a[1] = Character.valueOf('A');
a[2] = Float.valueOf(12.33);
a[3] = "Hello";
我没有遇到你描述的问题。我觉得是自动装箱,至少在我的配置环境下是这样。
public static void main(String[] args) {
// TODO Auto-generated method stub
Object a[] = new Object[4];
a[0] = 1; //while assigning integer value, am getting an error: "Type mismatch: Cannot convert Integer to Object
a[1] = 'A'; //while assigning char value, am getting an error: "Type mismatch: Cannot convert char to Object
a[2] = 12.33;//while assigning integer value, am getting an error: "Type mismatch: Cannot convert double to Object
a[3] = "Hello"; //Accepting only string values.
for(Object i:a) {
System.out.println(i.toString());
}
}
右键单击 -> 运行 作为 -> Java 应用程序, 控制台输出:
1
A
12.33
Hello
如果您使用低于 5 的编译器合规级别,这正是您会收到的错误消息。请注意,这与您系统上的 Java 版本不同。请在此处查看修复 .