Welcome to Dream.In.Code
Become a Java Expert!

Join 149,585 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,778 people online right now. Registration is fast and FREE... Join Now!




A Couple Things About Generics

 
Reply to this topicStart new topic

A Couple Things About Generics, generic methods and comparator param of type E

UnderTheBridge
5 Oct, 2007 - 12:05 AM
Post #1

New D.I.C Head
*

Joined: 4 Oct, 2007
Posts: 11


My Contributions
**Edit**
New question about generics in post 8.

I'm just learning generics but I have a good grasp on the idea, just not all of the syntax yet. In one class I have a public generic method and in my program I call that method and there is a problem with how I am trying to tell it my Comparator should be of type String.

CODE
//In the public class pClass
public static <E> void methodN(E[] e, Comparator<? super E> c){
..do stuff;
}

//calling it from the program(in a different class)
//A is a String array that is already filled
pClass.methodN(A,Comparator<String>);


I am using eclipse and the error says 'Syntax error on token ">", Expression expected after this token'. Anyone know my mistake? Thanks for any help.

This post has been edited by UnderTheBridge: 5 Oct, 2007 - 03:26 PM
User is offlineProfile CardPM
+Quote Post

Programmist
RE: A Couple Things About Generics
5 Oct, 2007 - 01:29 AM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Look at his code:
pClass.methodN(A,Comparator<String>);

You are passing in an Array, A, and...what exactly? Hint: Remember, this is a method call, not a method definition, so you have to pass actual instantiated objects.
User is offlineProfile CardPM
+Quote Post

UnderTheBridge
RE: A Couple Things About Generics
5 Oct, 2007 - 01:57 AM
Post #3

New D.I.C Head
*

Joined: 4 Oct, 2007
Posts: 11


My Contributions
That's where I'm confused what I'm supposed to be sending to the method. Am I supposed to be sending an instance of a String Comparator? I have no idea how to make an instance of a String Comparator lol.

a few of the million things Ive tried in the last hour:
CODE
Comparator c = new Comparator;
pClass.methodN(A,c);

//try 2
java.util.Comparator c = new java.util.Comparator;
pClass.methodN(A,c);


My guessing seems to be achieving nothing.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: A Couple Things About Generics
5 Oct, 2007 - 02:29 AM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
The Comparator is an interface, you must create a class that implements it, so you'll be able to instantiate that class (and pass the instance to your function ).
User is offlineProfile CardPM
+Quote Post

UnderTheBridge
RE: A Couple Things About Generics
5 Oct, 2007 - 02:50 AM
Post #5

New D.I.C Head
*

Joined: 4 Oct, 2007
Posts: 11


My Contributions
That makes perfect sense, I don't see how I didn't catch that hours ago haha. The only thing I still don't understand is what my homework is telling me to do because I know were not supposed to be writing anything that implements Comparator. Here's the line from our homework, what do you make of it? "method must use the Comparator passed in to make comparisons". Also, The method as I had it was written the way we were given it.

CODE
//In the public class pClass
public static <E> void methodN(E[] e, Comparator<? super E> c){
..do stuff;
}
Do you think maybe I'm suppose to just right this for someone who might later implement Comparator and need this method?
User is offlineProfile CardPM
+Quote Post

1lacca
RE: A Couple Things About Generics
5 Oct, 2007 - 03:31 AM
Post #6

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Without knowing your class and seeing the full text of the assignment, I would say that you should just complete that function, and you don't have to create a comparator - unless you have to call that function, because to do so, a Comparator is required. However to test your function, you'll need a comparator (java.text.Collator might work just fine), but probably you don't have to hand it in.

User is offlineProfile CardPM
+Quote Post

Programmist
RE: A Couple Things About Generics
5 Oct, 2007 - 08:19 AM
Post #7

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Exactly. You can implement your function to use a comparator, but when you actually call it, you have to have an instance of Comparator to pass to it. If you're not implementing it then you are using one that someone else implemented (like Collator as 1lacca mentioned).

This post has been edited by Programmist: 5 Oct, 2007 - 08:19 AM
User is offlineProfile CardPM
+Quote Post

UnderTheBridge
RE: A Couple Things About Generics
5 Oct, 2007 - 03:50 PM
Post #8

New D.I.C Head
*

Joined: 4 Oct, 2007
Posts: 11


My Contributions
Alright, thanks. New question though. I have two generic methods that both work the same way but one sorts only Comparable instances of type C and the other only sorts Comparator instances of type E. I need to reuse code and I'm not sure how to pass the object of type E or C to a new method.

I tried passing them as objects but then the method would not allow me to use things like compareTo saying it didn't know for sure that this Object was Comparable. Is this a way to use multiple wildcards to somehow make a parameter take in either a Comparable or a Comparator? If so I might still run into that same problem.

Otherwise, is there a better way to approach the whole idea of code reuse? I'm reading the java generics tutorial at http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf but don't yet see what can be used. If you could recommend another tutorial that explains this situation specifically it would be great.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: A Couple Things About Generics
5 Oct, 2007 - 10:53 PM
Post #9

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
QUOTE
I have two generic methods that both work the same way but one sorts only Comparable instances of type C and the other only sorts Comparator instances of type E.

Is the second method really supposed to sort Comparators?
User is offlineProfile CardPM
+Quote Post

UnderTheBridge
RE: A Couple Things About Generics
6 Oct, 2007 - 12:28 AM
Post #10

New D.I.C Head
*

Joined: 4 Oct, 2007
Posts: 11


My Contributions
Umm sorry no, but it is supposed to sort using a comparator of type E. Hey quick question, If I need to output to a file that doesnt exist, how do I create it cuz I'm having a heck of a time finding out and Im sure its because its so simple.

I have:
CODE
FileWriter f = new FileWriter (name);
        BufferedWriter out = new BufferedWriter(f);
But I get the error unhandled exception because it may not exist, How do I create it?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:57PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month