Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

How to write a String specific Comparator?

Thread view: 
hiwa - 31 Mar 2005 10:03 GMT
In the below code, argument type of compare() method is declared
as String. However, compiler emits the error:

IndexSort.java:25: cannot resolve symbol
symbol: method indexOf(String)
location class java.lang.Object

Java generics is very hard to master except the top level shallowest
user layer. Could someone help writing a String specific Comparator
class properly?
---------------------------------------------
01: import java.util.*;
02:
03: public class IndexSort{
04:
05:   public static void main(String[] args){
06:
07:     String[] sa
08:      = {"abasa", "dwapba", "oobaon", "balsa", "eglengelbao", "sveembat"};
09:
10:     Arrays.sort(sa, new IndexComparator<String>("ba"));
11:
12:     System.out.println(Arrays.deepToString(sa));
13:   }
14:
15:   static class IndexComparator<String> implements Comparator<String>{
16:     String str;
17:
18:     public IndexComparator(String subs){
19:
20:       str = subs;
21:     }
22:
23:     public int compare(String o1, String o2){
24:
25:       return o1.indexOf(str) - o2.indexOf(str);
26:     }
27:   }
28: }
----------------------------------
John McGrath - 31 Mar 2005 11:01 GMT
> In the below code, argument type of compare() method is declared
> as String.

Not exactly!

> However, compiler emits the error:
>
[quoted text clipped - 5 lines]
> user layer. Could someone help writing a String specific Comparator
> class properly?

> 10:     Arrays.sort(sa, new IndexComparator<String>("ba"));

> 15:   static class IndexComparator<String> implements Comparator<String>{

In this context, "String" does not refer to the class java.lang.String.
When you place <T> after the name of the class in the class declaration,
you are creating a generic class, with the generic type parameter <T>.
You can use whatever identifier you want there:  <T>, <Type> or <String>.

So you are defining a generic class IndexComparator, one that can be used
with Strings, Dates, or whatever.  I do not think that is what you want.

Try removing the <String>:

> 10:     Arrays.sort(sa, new IndexComparator("ba"));

> 15:   static class IndexComparator implements Comparator<String>{

Signature

Regards,

John McGrath

hiwa - 01 Apr 2005 00:43 GMT
> > In the below code, argument type of compare() method is declared
> > as String.
[quoted text clipped - 28 lines]
>
> > 15:   static class IndexComparator implements Comparator<String>{
Thanks John. It now works perfectly.
The result seems for me to have used only the shallowest part
of Java generics. :)
-------------------------------------------------------------
import java.util.*;

public class IndexSort{

 public static void main(String[] args){

   String[] sa
    = {"abasa", "dwapba", "oobaon", "balsa", "eglengelbao", "sveembat"};

   Arrays.sort(sa, new IndexComparator("ba"));

   System.out.println(Arrays.deepToString(sa)); // JDK 1.5
 }

 static class IndexComparator implements Comparator<String>{
   String str;

   public IndexComparator(String subs){
     str = subs;
   }

   public int compare(String o1, String o2){

     return o1.indexOf(str) - o2.indexOf(str);
   }
 }
}
--------------------------------------------------------------


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.