aspose file tools*
The moose likes Beginning Java and the fly likes Need Help Sorting ArrayList of Generic Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Help Sorting ArrayList of Generic Objects" Watch "Need Help Sorting ArrayList of Generic Objects" New topic
Author

Need Help Sorting ArrayList of Generic Objects

Todd Bruner
Greenhorn

Joined: Aug 23, 2006
Posts: 7
Hello and thanks in advance for your help...

I am running into a problem trying to sort an ArrayList of objects. First the Objects I'm trying to sort look like (paired down to the essentials for this post):


In another class the code that tries to sort this looks like:


where foo.getStuff() returns a collection of Node objects.

The error the compiler is giving me is:



I guess one fix would be to replace <V> with Node, but that defeats the purpose of Generics, right? While in this example, I'm working with Nodes, later I might want to do the same with "Things."

I'm trying to understand Generics put this one has me stumped. Thanks again for any help you can provide.

Todd

[edit]Add newlines to keep width of post on the screen. CR[/edit]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
You are setting up a List<V> but you haven't got a V class anywhere.
You ought to parameterise your compareTo method

public class Node implements Comparable<Node>
. . .
public int compareTo(Node nnn)
{
. . .
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

When you say

foo.getStuff() should return a collection of V or any subclass of V
For example, say you have three classes like this

and when you say




SCJP 5.0 and now fighting with ExtJS
Todd Bruner
Greenhorn

Joined: Aug 23, 2006
Posts: 7
Thanks for both of the posts!

I parameterized the Node object ( public Node implements Comparable<Node> ).

As for the foo.getStuff() it does return a collection of <V>. I guess what I meant to say was that I happen to know that the elements of the arraylist are "Nodes". This is where my understanding and my generic lingo gets in the way. Sorry.

I am still getting the Bound Mismatch error.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
. . . And you ought to declare and instantiate the List to take Nodes at present.
List<Node> nodeList = new ArrayList<Node>();

You do at present want to sort nodes. You could of course declare the List to take <T extends Comparable><? super Node>>.

Probably simplest to stick to List<Node> for the time being; if "Thing" extends Node, then you can add it to the same List. If it doesn't extend Node, or if the overridden compareTo method isn't compatible with the Node compareTo method, then you will get nasty Exceptions if you try to sort the Lists.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
If you want to sort Nodes, and the foo#getStuff method returns <V> then you would have to have Foo as a parameterised class, with a formal type parameter <V> and instantiate that as

private Foo<Node> foo;
. . .
foo = new Foo<Node>();

At least I think so; I am not sure on this point. You might be able to achieve what you want with type inference, which you will find from one of the links below, under "methods and constructors."

The whole idea of generics is to maintain type-safety by restricting the different types permitted in a particular context. I presume you have been through the Java Tutorials, here and here, where you find "methods and constructors". And also do a Google search for "Angelika Langer Java Generics" where you will find lots of useful information.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help Sorting ArrayList of Generic Objects
 
Similar Threads
generics
generics
Generics warning
Understanding Generics
Generics and Comparable