I have an Object[] in Java and want to convert it to 开发者_开发知识库IProject[], which is a Java interface (org.eclipse.core.resources.IProject), in order to write a plugin for eclipse.
Is this possible?
Regards
You can't convert the array itself - arrays know the type of their slots, so you can't just cast an instance of Object[]
to an expression of type IProject[]
, even if the array happens to contain only instances of IProject
(unless you happen to have a variable of type Object[]
which actually points to an instance of IProject[]
).
Instead, you'll need to make a new array with the same contents:
Object[] objects;
IProject[] projects = new IProject[objects.length];
System.arraycopy(objects, 0, projects, 0, objects.length);
Array stores are dynamically type-checked, so if your Object[]
contains any objects which are not instances of IProject
, you'll get an ArrayStoreException
.
are the Objects in Object[]
instances of IProject
?
see casting Object array to Integer array error
精彩评论