Convert object to object []

the method inside the library I'm using returns an object, but it is really an array of objects. And I'm stuck where I need this value. It is strange that no one asked for such a conversion.

+3
source share
4 answers
    Object[] objects = new Object[] {1,"fde", 5L};
    Object casted = objects;
    Object[] recovered = (Object[]) casted;
+7
source
Object o = ...
Object[] array = { o };
+2
source

API Object, , . , . getter style - .

"". , .

Object object = MyApi.getFoo();
Object[] theRealThing = (Object[]) object;

, , :

Object object = MyApi.getFoo();
if (object instanceof Object[]) {
    Object[] theRealThing = (Object[]) object;
} else {
    System.out.println("The impossible has happened!"); // or something like that
}
+2

:

Object[] arrayOfObjects = (Object[]) yourMethod();
+1

Source: https://habr.com/ru/post/1783070/


All Articles