DewinDell
DewinDell

Reputation: 1028

Create array of same type as passed to method

I have in java a sorter class that takes an array of comparable objects and sorts them. I need to create a temp array for the sort (this is merge sort) and the array needs to be the same type as the type passed in (I think?) I'm getting all sorts of warnings from eclipse no matter what I do.

My method declaration is

public <E extends Comparable<? super E>> void sort(E[] data);

I read about generics a bit but I'm still confused. Any links to a complete generics tutorial would also be a lot of help. Thanks.

Upvotes: 1

Views: 107

Answers (1)

Alexei Kaigorodov
Alexei Kaigorodov

Reputation: 13525

No need to use generics here, and no need to create the temp array of the same type as the type of the argument.

public void sort(Comparable[] data) {
  Comparable[] temp=new Comparable[data.size()];
  ...
}

Upvotes: 4

Related Questions