Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java how to: Generic array creation from class with generic?

Tags:

java

generics

I have a class FirstClass<O> and a class SecondClass<O>, and I want to make an O[] in SecondClass<O> inside a routine which is called from FirstClass<O>, where O is a generic class parameter. I fail to find how to do this.

I need an O[] specifically (and not ArrayList<O> or similar) because I need to get elements from it very often inside the body of a loop, and it matters for the execution time of my algorithm.

So I would want something along these lines.

public class FirstClass<O> {
    void someRoutine(n and other params) {
        //Do some stuff
        SecondClass<O> = new SecondClass(n, possibly_other_params);
        //Do some stuff
    }
}

and

public class SecondClass<O> {
    O[] myArray;
    SecondClass<O>(int n, possibly_other_params) {
        //Here the constructor that creates an O[n]
    }
}

Some methods I found on the web, but do not work for my case:

  • Use O[] array = (O[]) new Object[n]; but that doesn't compile.
  • Use Object[] array = new Object[n]; and do an (O) cast every time I request something from the array, but this is way too slow
  • Use Array.newInstance(Class<O> type, int n);, with O o; and type=o.class but it complains that type is now of type Class<CAP#1> instead of type Class<O>, whatever CAP#1 means...

How should I do this properly in Java, with optimal execution speed in mind?

like image 384
user1111929 Avatar asked Dec 13 '25 12:12

user1111929


2 Answers

Java's handling of generics is in pretty rough shape but you can accomplish something close to what you want if you are prepared to make small adjustments. Please see:

public class FirstClass<O> {
    Class<O> type;

    public static <O> FirstClass<O> create(Class<O> type) {
        return new FirstClass<O>(type);
    }

    public FirstClass(Class<O> type) {
        this.type = type;
    }

    public void routine(int size /*, other params */ ) {
        SecondClass<O> instance = new SecondClass<O>(type, size);
    }
}

public class SecondClass<O> {
    public O[] array;

    @SuppressWarnings("unchecked")
    public SecondClass(Class<O> type,int size) {
        array = (O[])Array.newInstance(type,size);
    }
}

And a use case:

FirstClass<Integer> instance = FirstClass.create(Integer.class);
instance.routine(110);

This is just a rough example although I am sure you could accomplish something similar without having to use this approach.

Use O[] array = (O[]) new Object[n]; but this requires an (O) cast every time I request something from the array, so this is way too slow

What? The whole point of type O[] is that you don't need an (O) cast when getting things out of it

like image 25
newacct Avatar answered Dec 15 '25 01:12

newacct



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!