• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to write generic method for this method?  RSS feed

 
Prosenjit Banerjee
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Please help me out telling how to write and call a generic method for the following method. The actual problem with this method is that I am not passing any generic parameter through the method but the method uses a generic type witihin its definition. I don't know how to do it by making a generic class and how by making a generic method.





If you want to get an idea of the whole program here it is
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Starting from Java SE 5, Vector class is generic. When you construct a new Vector instance, you can do this Vector<String> v = new Vector<String>(10);

Don't forget to change the method parameters that take the generics types into something like this:
public static int arrangeStrings(Vector<String> v, int p)

For more information about generics. Check this tutorial:
http://java.sun.com/docs/books/tutorial/java/generics/index.html

Hope that helps.
[ June 21, 2007: Message edited by: Freddy Wong ]
 
Prosenjit Banerjee
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. But I want to make a vector that is a collection of various kinds of objects. Just have a look at my full program to understand what am I trying to do. Now, tell me how to do it?

Moreover I didn't question about Vectors rather I asked about how to write and call a generic method through I won't pass a generic parameter, but use a generic type within the definition.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use the link given to you by Freddy, it tells you all about generics including working with generic methods, and whildcards.

have fun

Gavin.
 
Prosenjit Banerjee
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Freddy and Gavin.
I have gathered some knowledge with the help of you and look how strangely I soved my problem. Please leave your valuable comments on this.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Minor comment:

Instead of using string literals to represent classes, you could use class literals. The current commented-out implementation to check for type instances based on the ending class name segment wouldn't work given instances of two unrelated classes, where the name of one of them happens to end with the complete name of the other (e.g. A and AA, or Point and CounterPoint).

Don't read the next paragraph if you'd rather investigate and discover on your own how to use class literals and methods of Class objects.

To clarify my minor comment above, arrange(String className, Vector v, int p) could be changed to arrange(Class clazz, Vector v, int p), and Test.arrange("String", v, p); could be changed to Test.arrange(String.class, v, p);. Then, you could use the isInstance(Object) method available to clazz.
 
Prosenjit Banerjee
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Mr. Schreckmann, your comment really proved valuable to me. You are absolutely right and I got what I should do to solved my own created problem. Thank you very very much.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read your cod a few times, but I don't understand exactly why you need to test the type of the instances in the Vector in your method. Anyway you could do this:

You could make it even more generic by allowing the method to accept a List instead of a Vector.
[ June 21, 2007: Message edited by: Garrett Rowe ]
 
Prosenjit Banerjee
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Garrett Rowe for your suggestion.
I have made attemts to test the type of the instances because if you read the demo main() function carefully then you will get the fact that the vector v is actually a collection of various types of objects at the same time. For example say at a particular moment the vector holds the following objects:
A String
A Point
A String
A Double
A Point
A Double
A String
...

I want to arrange these objects so that they come in an order as shown below:
A String
A String
A String
A Point
A Point
A Double
A Double
...

i.e., the same kind of objects come one after another. I know list is a better approach but I want to see whether it is possible by using a vector. Now, you tell me won't I have to check the type of each element of the Vector?
To do this how come your code works? Your arrange() straightway swaps each and every element of the vector.
And, why have you made the swap() function generic. Doesn't it work very with without making it generic as I wrote?
[ June 21, 2007: Message edited by: Prosenjit Banerjee ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I misunderstood your intentions at first. Generics are all about increasing type safety in code, but if you have a vector that has Doubles, and Integers, and Points in it that you have to inspect and cast before they are used, then that is not really a type safe Vector and generics don't seem to apply.

I understand that this is probably an academic question, i.e. "Can this be done?", rather than a practical one "Should this be done?". In my opinion the answer to the first question is "Yes", while the second is an emphatic "NO!".

Here's another way of getting the output you're looking for:
Its a hack that relies on the fact that coincidentally, the order you are looking for is a reverse alphabetical order of the class names, but it works.

EDIT: Well i guess it doesn't work now that I look closer, you wanted Strings->Points->Integers, and this gives Strings->Integers->Points. Oh well, back to the old drawing board.
[ June 22, 2007: Message edited by: Garrett Rowe ]
 
Prosenjit Banerjee
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Garrett Rowe. I have learnt something new from you. But it was unfortunate that my illustration shows the elements should be arranged in reverse alphatical order or corresponding class names. Whereas the real fact has been shown in the actual program written by me.
I wanted to say that my Vector may contain objects of any damn classes but they would be arranged in the order the author of the main() function wants.
So, the code written by you actually does not server my purpose. But I again want to thank you a lot.
 
Francesco Bianchi
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prosenjit Banerjee:
Thanks again Garrett Rowe. I have learnt something new from you. But it was unfortunate that my illustration shows the elements should be arranged in reverse alphatical order or corresponding class names. Whereas the real fact has been shown in the actual program written by me.
I wanted to say that my Vector may contain objects of any damn classes but they would be arranged in the order the author of the main() function wants.
So, the code written by you actually does not server my purpose. But I again want to thank you a lot.


Did you consider using a SorteMap implementations with a key representing an object type (maybe a String matching the class name) and Lists as values? This way you could organize you orbject by class, using one comparator for ordering the "objects sets" by class, and n-different comparators for sorting every kind of array the way you prefer. What I mean is something like SortedMap<String, List<?>>.And if you need to get the whole Map content back as a List, you can extends an implementation of SorteMap whoch values() method goes on recursively returning a list made of the union of all the lists contained. Hope I have been of any help

Cheers
 
Prosenjit Banerjee
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bianchi. But your suggestion seemed too high in level and complicated to serve my purpose. I don't think that it is easier than useing getClass() method what I have done. So, I would like to stick to my way so far.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!