How do i cast an object to a List<Object[]> type?

Maurice :

I have a situation where i get an Object as a return type and it needs to be casted to a List<Object[]> type. This looks easier then it seems though.

First of all when i try the following: Object[] objectArray = (Object[])object; i get the warning that the (Object[]) cast is redundant. Why on earth would this be redundant? An Object surely must be something different to an Object Array right? Well according to Intellij its secretly the same thing and i would appreciate it if someone could explain me why this is the case.

Because of the above described phenomenon i can't do this either:

Object[] objects = (Object[])callOutputs.getOutputParameterValue(entrySet.getValue());
List<Object[]> objectList = Arrays.asList(objects);

Despite casting the returntype explicitly to Object[], Arrays.asList() still treats it as an Object. Even though the variable type is Object[].

Can someone explain how you should create a List<Object[]> object?

EDIT: Here is the method declaration:

public abstract <T> T getOutputParameterValue(org.hibernate.procedure.ParameterRegistration<T> parameterRegistration)
ernest_k :

I'm assuming that entrySet.getValue() returns ParameterRegistration<Object[]> (otherwise there would be a compiler error). That being the case,

Object[] objects = (Object[])callOutputs.getOutputParameterValue(entrySet.getValue());

is casting Object[] to Object[]. getOutputParameterValue() returns the type of its input's type argument (The T in ParameterRegistration<T>), so the compiler can check that the return type is Object[] and you don't need to explicitly cast it.

The Arrays.asList() thing is a different story.

Arrays.asList(Object[]) is expected to return List<Object>, because the method is declared to take an array of type T, and returns a List<T>.

If you want it to be a List<Object[]>, you can force it by adding a type witness

List<Object[]> objectList = Arrays.<Object[]>asList(objects);

This is needed only when you have a single argument (it wouldn't be needed if you had something like Arrays.<Object[]>asList(objects, objects1, objects2))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=295334&siteId=1