Casting an array of Objects into an array of my intended class

52,983

Solution 1

Because toArray() creates an array of Object, and you can't make Object[] into DataObject[] just by casting it. toArray(DataObject[]) creates an array of DataObject.

And yes, it is a shortcoming of the Collections class and the way Generics were shoehorned into Java. You'd expect that Collection<E>.toArray() could return an array of E, but it doesn't.

Interesting thing about the toArray(DataObject[]) call: you don't have to make the "a" array big enough, so you can call it with toArray(new DataObject[0]) if you like.

Calling it like toArray(new DateObject[0]) is actually better if you use .length later to get the array length. if the initial length was large and the same array object you passed was returned then you may face NullPointerExceptions later

I asked a question earlier about Java generics, and was pointed to this FAQ that was very helpful: http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

Solution 2

To ensure type safety when casting an array like you intended (DataObject[] dataArray = (DataObject[]) objectArray;), the JVM would have to inspect every single object in the array, so it's not actually a simple operation like a type cast. I think that's why you have to pass the array instance, which the toArray() operation then fills.

Solution 3

After Java 8 with introduction of streams and Lambda you can do the following too:

For casting a normal Array of objects

Stream.of(dataArray).toArray(DataObject[]::new);

For casting a List

dataList.stream().toArray(DataObject[]::new);
Share:
52,983
Mona alawfi
Author by

Mona alawfi

Solving problems.

Updated on July 09, 2022

Comments

  • Mona alawfi
    Mona alawfi almost 2 years

    Just for review, can someone quickly explain what prevents this from working (on compile):

    private HashSet data;
    
    ...
    
    public DataObject[] getDataObjects( )
    {
        return (DataObject[]) data.toArray();
    }
    

    ...and what makes this the way that DOES work:

    public DataObject[] getDataObjects( )
    {
        return (DataObject[]) data.toArray( new DataObject[ Data.size() ] );
    }
    

    I'm not clear on the mechanism at work with casting (or whatever it is) that makes this so.

  • Mona alawfi
    Mona alawfi over 15 years
    Ok. Is there any way to cast Object[] to DataObject[] then? I don't understand what that function exists for, otherwise. Sorry I'm trying to get these stupid questions out of the way now so they don't haunt me later.
  • Paul Tomblin
    Paul Tomblin over 15 years
    I'm afraid there isn't. You just have to use the other toArray call.
  • Henning
    Henning over 15 years
    No, you can just create a new array and cast your objects one by one. In Java, arrays are hardly ever the way to go, though.
  • Hot Licks
    Hot Licks over 10 years
    You can, sort of, "cast" Object[] to DataObject[] using System.arraycopy.