0
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.

} 
4
  • What is interface B? Is it relevant to the question? Commented Nov 26, 2012 at 4:31
  • Are you sure your assignment isn't to sort the given array in place? Commented Nov 26, 2012 at 4:35
  • @Bohemian B<Integer> b1 = new haha<Integer>(); // inside of BTester class Commented Nov 26, 2012 at 4:38
  • @Perception I'm sorry. I don't understand your question. Sort the given array in place?? Commented Nov 26, 2012 at 4:44

1 Answer 1

1

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:

public class Haha<E extends Comparable<? super E>> {
    public static <T extends Comparable<? super T>> void sort (T[] a) {
        // unusual, but can be done
        final Haha<T> haha = new Haha<T>(a);
    }

    private E[] array;

    public Haha() {
        this(new Comparable[10];
    }

    public Haha(final E[] array) {
        super();
        this.array = array;
    }
}

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the reply! I'm little confused here. Isn't Haha<T> haha = new Haha<T>(a); should be stated inside of private Haha(??) ??
because "..define a private constructor that the static sort() method can call to create a Haha object initialized with a particular given array"
Who wrote that requirement? You cannot access a private method of a class from within a static method, unless the private method is static itself.
As you wrote, inside of static sort() { Haha<T> haha = new Haha<T>(a);} and I tried inside of private Haha(E[] b) { System.out.print(b[0]);}. it actually printed out "1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.