Trouble with generic arrays

Started by
6 comments, last by TheChubu 10 years, 8 months ago

Hi! Well, I'm having an issue with a generic array for a class I implemented.

I wanted to implement a vector that held generic types. Its just a very thing wrapper around an array to work as a base for a matrix class.

I did the following:


public class CmpVector<Type>
{
    public final Type[] array;  

    @SuppressWarnings("unchecked")
    public CmpVector ( final int size )
    {
        this.array = (Type[]) new Object[size];
    }

    public Type getValueAt ( final int index )
    {
        return this.array[index];
    }
    
    public void setValueAt ( final int index, final Type val )
    {
        this.array[index] = val;
    }
}

(i'm omiting other irrelevant methods/constructors)

I noticed that the stock JDK "ArrayList" implementation used instead an Object[] for storing the values, doing casts to "T" back and forth in all accessor and mutator methods. That's probably because you can't instantiate a new generic array in Java due type erasure (ie, new Type[size] gives a compiler error), but I can declare a generic array reference.

So I thought, instead of creating an array of Object, I could declare an array of Type, give it a new array of Object, and as long as I store Type objects in it, I shouldn't have issues. Besides, it would work with my original idea of having the array public, since if I made an Object[] array instead, it would need to be private so you just can't assign any object to it.

Now enter the static generic method for filling arrays:


public static <Type> void arrayFill ( final Type[] array, final Type value )
    {
        if ( array.length > 0 )
        {
            array[0] = value;
        }
        
        for ( int i = 1; i < array.length; i += i )
        {
            System.arraycopy( array, 0, array, i, ( ( array.length - i ) < i ) ? ( array.length - i ) : i );
        }
    }

This method works with arrays directly, thus no Vector<Type> interface.

It was all good and well until I made the following call:

arrayFill(particleVec.array, null);

I'm sending the Vector<ParticleTag>'s array and a null to the method, so it fills the array with null values. I get a "ClassCastException" saying that I can't cast an Object to a ParticleTag. ParticleTag is an interface that RidgeParticle and RiverParticle implement (yes, its from my terrain generator biggrin.png ).

The thing that I don't understand is that setValueAt(index, particle) method works fine. If I do a getValueAt(index) I get the ParticleTag just fine too. But when I operate with the array directly, ClassCastException.

I have another method that receives a specific type of array (so no generics there), and I can't pass it the array of Vector<Type> either. I made an arrayFill as method of the Vector<Type> classe and it works fine that way.

I don't understand what's so special about the object itself that working on the array inside Vector through its methods works fine, but working with the array outside of the object doesn't. In my mind either it would work fine on both cases or it wouldn't work at all. At worst it should make a downcast from Object to ParticleTag, but that should be fine since they do are ParticleTags.

Any ideas?

EDIT: I definitively can't access the array at all, for example, doing a simple:


System.out.println ( new CmpVector<ParticleTag>(100).array.length);

throws a ClassCastException.

While doing it like this:


System.out.println ( new CmpVector<ElevationState>(100).getLength() );

prints 100 as expected (and the getLength() implementation is just a return array.length)

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Advertisement

It puzzled me for a while, but I've come up with the following explanation for the length-related problem. It seems to make correct predictions.

In order to understand what's going on, you need to know the details of how java works. You probably know this already, but I'm mentioning it for completeness and clear context establishment (i.e. indicating which java 'features' come into play).

First, there's the type erasure (which you mentioned yourself): every type parameter gets replaced by Object (in your code at least, because you didn't specify bounds). Java will put in runtime type checks where it has enough information to do so. In a generic method m<T>, the compiler knows nothing about T, hence it cannot add runtime checks. In a method that uses CmpVec<Integer> however, in knows T = Integer, so runtime checks will be added.

Second, an Object[] cannot be cast to e.g. a String[]. AFAIK, java guarantees that reading from a T[] array yields T objects. If casting Object[] to String[], this would not hold. However, you can cast String[] to Object[], since java makes no guarantees about writes: given a T[] array, it might be that writing a T to the array fails at runtime (while the code passes the compiler type checks). This is because arrays keep track of which type they're supposed to hold at runtime.

Putting these two facts together now. In your code, inside CmpVector, the compiler knows nothing about T, so the array field is really an Object[] at runtime. Your length-printing code contains an explicit type: ParticleTag, which leads the compiler to add a cast to ParticleTag[] for 'safety'. So, the generated code contains a cast of an Object[] to a ParticleTag[], which fails.

If you were to print the length more indirectly, hiding the actual type from the compiler, it would work again:


public static <T> void indirect(CmpVector<T> v)
{
  System.out.println( v.array.length );
}

indirect( new CmpVector<Whatever>(5) ); // works

This does not yet explain why the fill fails though. Probably something similar.

-- Edit

The ClassCastException happens where the arrayFill method is called, so it probably means that every time you use the array field directly, a runtime check will occur, which will fail. The arrayFill works if I put it inside a generic method similar to 'indirect' as shown above.

If you're determined to keep the array field public, I'd suggest being 'honest' to the compiler, and just tell it is a Object[] array, not a T[] array. You'll have to suppress unchecked cast warnings at multiple places in your code though (or you could create an auxiliary @SuppressWarnings("unchecked") T[] arr() { return (T[]) array; } method).

Hm, I think I understand now.

I thought the downcast from Object[] to ParticleTag[] would be fine as long as I don't put anything besides ParticleTags inside. Turns out it doesn't works like that then.

Of the top of my head maybe I *could* make it work using the reflection api, kinda a heavy handed workaround though (aka, "f you java! I'm hacking my way around you!" )

So the "solution" is to use Object[] and assign to it (Object)whatever only. I don't quite like it but it is what it is, thanks for the answer!

EDIT: Yep, doing this works:


public final Type[] array;
 
@SuppressWarnings ( "unchecked" )
public CmpVecReflect ( final int size, final Class<Type> type )
{
array = (Type[]) Array.newInstance( type, size );
}
 
// Then in the code
 
CmpVecReflect<Float> vec = new CmpVecReflect<>( 10, Float.class );
System.out.println(vec.array.length);

I'd have to pass the class type every time though... I'm not sure if I like it. I do have a CmpVecFloat that only uses float biggrin.png This one is just as an example.

The thing is that these objects should be very generic. And probably I'll be using them a lot. Specially since I'm using the derived class, CmpMatrix for storing a 2d surface of particles, states, velocities, etc. First I had a class that would hold an array of each of these things, and provide get/set methods for each accordingly. Which ends in a bunch of "getHeightAt()" "getVelocityAt" "getStateAt" and so on. Its messy and its the same implementation for all of them except the difference is the return type.

So I thought, I'll have matrix/vector classes for primitive types, and generic ones for reference types. That saves up a lot of boilerplate. But if its going to work with all these quirks, then I'm not so sure if it would be a good idea.

EDIT2: Well, I'll have both. One that uses reflection with ugly constructors and has a proper backing array, and one that doesn't uses reflection but with proper constructors and with the ugly Object array. Default constructor on the reflection classes will backfire in your face if you dare to access the array directly, which is kinda fun in some sense. I'll probably regret this sometime in the future :D

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

My personal solution is to stay away from java as much as possible. I hate the language with a passion, given how it works against me every time I try to implement something nontrivial. The main challenge when using it is to find the least ugly solution.

I wish you luck with your battle against the language. You're a braver person than I am.

If you want collection classes, you should just use List/ArrayList. and Arrays.asList() and Collection.toArray(); using arrays directly is also reasonable. Your home made vector class doesn't appear to have a valid purpose.

Omae Wa Mou Shindeiru

My personal solution is to stay away from java as much as possible. I hate the language with a passion, given how it works against me every time I try to implement something nontrivial. The main challenge when using it is to find the least ugly solution.

I wish you luck with your battle against the language. You're a braver person than I am.

What can I say. I like it. Its pretty simple for most of the time until you hit the rough corners like these. Sadly its kinda on its way to get complex syntax-wise...

If you want collection classes, you should just use List/ArrayList. and Arrays.asList() and Collection.toArray(); using arrays directly is also reasonable. Your home made vector class doesn't appear to have a valid purpose.

It has a very tiny purpose :D Its purpose its to be the superclass of CmpMatrix. Just as CmpVecFloat us the superclass of CmpMatFloat and so on.

I needed a two dimensional structure for references, so I followed the same idea of my other vector/matrix classes and made the generic vector class, and a matrix class that inherits from it.

They all provide more or less the same interfaces (except for "setIdentity()" because it doesn't makes much sense in this case :P ), for ease of use mostly.

I felt that using an ArrayList for this would be too much. I only wanted a two dimensional structure, a few methods and the possibility of using the class with the other utilities I had for the other vector/matrix classes. I don't need the CmpVector right now but maybe I'll find a use for it later, for now it serves as a base for the CmpMatrix class.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator


Its purpose its to be the superclass of CmpMatrix. Just as CmpVecFloat us the superclass of CmpMatFloat and so on.

I needed a two dimensional structure for references, so I followed the same idea of my other vector/matrix classes and made the generic vector class, and a matrix class that inherits from it.
Usually, row vectors, column and square matrix classes might inherit from a rectangular matrix class because they are special cases (they have additional operations, like dot product, inverse, determinants etc.); the opposite is wrong (a rectangular matrix isn't a vector).

Using generic matrix classes is also questionable; what data types other than double are they going to contain?

Omae Wa Mou Shindeiru

Usually, row vectors, column and square matrix classes might inherit from a rectangular matrix class because they are special cases (they have additional operations, like dot product, inverse, determinants etc.); the opposite is wrong (a rectangular matrix isn't a vector).

Well, its not a single vector but various vectors that define the main axis of once space in terms of another space, at least for transformation matrices. I can't reflect that with pure inheritance and trying to emulate it with composition would lead to very, very ugly classes.

Plus the subclassing from vector permits to reuse certain operations they both have in common, like addition, substraction, scalar multiplication and scalar division. Concrete example, scalarMultiplication(CmpVecFloat vec, float scalar) works the same for both CmpMatFloat and CmpVecFloat.

Though to be fair, that only works/makes sense on the float vectors and matrices that work in my math library. In this case right now (reference vector/matrix), like I mentioned, its just for unifying it under a common interface (getValueAt, setValueAt, fillWith, and other equally named methods among all of these classes). CmpVecFloat its a different class than this CmpVector<T>, and CmpMatFloat is a different class than CmpMatrix<T> but they're all used in the same manner.

Using generic matrix classes is also questionable; what data types other than double are they going to contain?

Let me see... The reference types I'm storing in this class are: Elevation state per height value, particle at each height value and velocity vector at each height value.

As I said, I needed a two dimensional structure for a terrain generator I'm doing. Already had those defined just for float primitives, so I made these classes that work in a similar way for reference types.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement