Integer[] intArray = new Integer[] {1,2,3,4};
Haha.sort(intArray);
public class Haha<E extends Comparable<? super E>> implements B<E>
{
private E[] array;
public Haha()
{
array = (E[]) new Comparable[10];
}
private Haha( ??? )
{
???
}
public static <T extends Comparable<? super T>> void sort (T[] a)
{
Haha(a);
}
}
I want to define a private constructor static sort() method can call to create a Haha object initialized with a particular given array.
But it has to sort its argument array without allocating another array because public constructor is already allocating another array.
problem 1)
How can private Haha receive ‘a’ from sort() method??
If I do
private Haha(E[] b) // gives error because of different type T & E
{
// skip
}
If I do
private Haha(T[] b) // gives error too because Haha is class of Haha<E>
{
// skip
}
problem 2)
How to initialize object with a particular given array
private Haha( ??? b ) // if problem 1 is solved
{
array = b; // is this right way of initializing array, not allocating?
// It also gives error because sort() is static, but Haha is not.
}
I question the overall design of this class, as it seems to me the assignment is simply to sort an array in place. However, note that the generic parameter type specified in your static method is entirely different from the one defined in your class signature. You can use both:
But for this to work, you need to make your alternate constructor public. This is because you are trying to access it from a static method, which is effectively the same as trying to access it from an entirely different class.