I'm trying to use generics in some methods and Netbeans is telling me I'm doing it wrong. It tells me that the lines ***Data = input is wrong and return ***Data[index] with errors to the effect of,

required: byte[]
found: T[]

or

required: T
found: byte

Is there a way to make what I'm doing work or are generics simply not going to work here? I've tried wrapping the primitives in wrappers.

private byte[] byteData;
    private short[] shortData;
    private int[] intData;
    private long[] longData;
    private float[] floatData;
    private double[] doubleData;

    private int[] dimensions;
    private int[] strides;

    private int start, length;

    private char type;

    public <T extends Number> MyArrayOld(T[] input, int start, int[] dimensions, char type){
        this(dimensions, type);
        this.start = start;
        switch(type){
            case '1':
                byteData = input;
                break;
            case 's':
                shortData = input;
                break;
            case 'i':
                intData = input;
                break;
            case 'l':
                longData = input;
                break;
            case 'f':
            case 'F':
                floatData = input;
                break;
            case 'd':
            case 'D':
                doubleData = input;
                break;
        }
    }

@Override
    public <T extends Number> T get(int index) {
        switch(type){
            case '1':
                return byteData[index];
            case 's':
                return shortData[index];
            case 'i':
                return intData[index];
            case 'l':
                return longData[index];
            case 'f':
            case 'F':
                return floatData[index];
            case 'd':
            case 'D':
                return doubleData[index];
        }
    }

I personally feel that the design is a bit weird:
- A class called MyArray which has member variables as arrays of all types
- Constructor accepting an array of type T (a specific type) along with accepting a `type` parameter?

Regarding the code; byteData = input won't work because input is an array of type T which in turn can be anything which extends Number (Float, Integer etc.) whereas byteData is an array of byte. A very similar reasoning with the `get` method; switching on `type` which is a value obtained at runtime just won't cut in. Generics is implemented using erasure; all the type hints you specify are compile-time only. Hence any use of generics which relies on the values of variables at runtime won't work.

How about something simple like:

class WrapperTest<T extends Number> {
    
    private T[] array;
    
    public WrapperTest(T[] source) {
        array = source;
    }
    
    public T get(int idx) {
        return array[idx];
    }
    
}

WrapperTest<Integer> wt = new WrapperTest<Integer>(new Integer[] { 1, 2, 3 }); // OK
WrapperTest<String> st; // won't work; String doesn't extend Number

Knowing the specific requirement in this case will help those reading this thread suggest a better alternative (memory/time efficient) if one exists.

I agree with everything the previous poster wrote. However, if you are stuck with this design for some reason, you can probably get it to compile and run by adding some casts. Did you try byteData = (byte[]) index; ?

I kind of abandoned this course of design in favor of a byte buffer and extensive use of the Number class, but I thought I should at least come back and say thanks for the answers, and if your sill curious the goal was to create a n-dimensional array class in Java of any number type, kind of like matlab.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.