• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bounded generic method

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd want to write a method for returning a sorted list from a passed one.
This is more easier to write than to explain :-)

Everything seems quite straightforward, but I don't understand why the compiler seems not to connect the input type to the output.

Thanks!
Saverio M.

------------------------------------------------------------------

class C2 implements Comparable { ... }

class ArrayComparator<T extends Comparable> implements Comparator<T[]> { ... }

public static void main(String... args)
{
List<C2> list = new ArrayList<C2>();
List<C2> listCopy = getSortedCopy(list);
}

private static <T extends Comparable> List<T[]> getSortedCopy(
List<T[]> data)
{
List<T[]> dataCopy = new ArrayList<T[]>(data);
Comparator<T[]> arrayComparator = new ArrayComparator<T>(dataCopy);
Collections.sort(dataCopy, arrayComparator);

return dataCopy;
}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm... I'm not sure just what you're trying to do here. It looks like the main problem is that getSortedCopy() expects its argument to be a List<T[]> where T extends Comparable, you're trying to pass it a Comparable. C2 has nothing to do with Lists or arrays, and so it can't match the type of the getSortedCopy() method. Probably you need to change C2 to be some sort of List<Foo[]> where Foo extends Comparable. Or maybe you need to change the signature of getSortedCopy() to be simpler. I really can't tell which is appropriate here; the pieces don't fit together, and I can't tell what the intent is.

By the way, it's usually a good idea to specify a generic parameter whenever a class expects one. E.g. Comparator and Comparable both have type parameters, so use theM> Don't say

but rather

And instead of

use

That way the compiler will have access to better information about what you're really doing.

Also, in general, mixing generics and arrays can be confusing, as they follow different rules. For example you can create a new ArrayList<T>, but you can't create a new T[] array. The reasons are somewhat subtle, but you're usually better off not mixing them. Typically in code that uses generics, you can just replace any array with a List. So List<T[]> can become <List<List<T>>, and Comparator<T[]> can be Comparator<List<T>>. This may not make an immediate difference in the code show, but it will probalby help if things become more complex.
 
Saverio Miroddi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!

I find the error. I wrote:



instead of:



because the method signature:



requires a list of comparable arrays, not a list of comparables!

---

the methods simply create a sorted copy of a list.
there is no array instantiation because the array objects are the same, just contained in different order in different lists.

Saverio!
reply
    Bookmark Topic Watch Topic
  • New Topic