| Author |
Generics, arrays, and sorting
|
Fritz Todd
Greenhorn
Joined: Apr 13, 2007
Posts: 7
|
|
Hello, colleagues. I am a tenderfoot here on the ranch and have a couple of problems that have been vexing me. One is a generics-arrays-sorting problem. In the following class, I generate an ArrayList of arrays of integer indices for chopping up a search string. To run it, compile the following code and enter a string to search and a list of search terms. In the version shown, which works, it will show you how the string to search contains the search strings you entered. (Enter "q" when it asks for another string to search to exit the application.) import java.io.*; import java.util.*; import java.util.regex.*; public class ChopPointTest { public static void main( String arg[] ) { BufferedReader cin; String stringToSearch, choppedString; StringTokenizer tokenizer; Pattern pattern; Matcher matcher; ArrayList<int[]> chopPoints = new ArrayList<int[]>(); // ChopPointComparator myComparator<int[]> = new ChopPointComparator<int[]>(); try { cin = new BufferedReader( new InputStreamReader( System.in ) ); System.out.print( "Enter a string to search: " ); stringToSearch = cin.readLine(); while ( !stringToSearch.equals( "q" ) ) { chopPoints.clear(); System.out.print( "Enter a list of search terms: " ); tokenizer = new StringTokenizer( cin.readLine() ); while ( tokenizer.hasMoreTokens() ) { pattern = Pattern.compile( "\\Q" + tokenizer.nextToken() + "\\E", Pattern.CASE_INSENSITIVE ); matcher = pattern.matcher( stringToSearch ); while ( matcher.find() ) { chopPoints.add( new int[] { matcher.start(), matcher.end() } ); } } /** * To get a version that does not work, comment out next method invocation and uncomment * all the other commented code. */ Collections.sort( chopPoints, new Comparator<int[]>() { public int compare( int[] left, int[] right ) { if ( left[ 0 ] == right[ 0 ] ) { return left[ 1 ] - right[ 1 ]; } return left[ 0 ] - right[ 0 ]; } } ); // Collections.sort( chopPoints, myComparator ); cleanup( chopPoints ); choppedString = stringToSearch.substring( 0, chopPoints.get( 0 )[ 0 ] ); for ( int i = 0; i < chopPoints.size(); ++i ) { choppedString += "[" + stringToSearch.substring( chopPoints.get( i )[ 0 ], chopPoints.get( i )[ 1 ] ) + "]"; if ( i == chopPoints.size() - 1 ) choppedString += stringToSearch.substring( chopPoints.get( i )[ 1 ] ); else choppedString += stringToSearch.substring( chopPoints.get( i )[ 1 ], chopPoints.get( i + 1 )[ 0 ] ); } System.out.println( choppedString ); System.out.print( "Enter a string to search: " ); stringToSearch = cin.readLine(); } } catch ( Exception e ) { e.printStackTrace(); } } static void cleanup( ArrayList<int[]> chopPoints ) { int[] thisChopPoint; int[] nextChopPoint; for ( int i = 0; i < chopPoints.size() - 1; ++i ) { thisChopPoint = chopPoints.get( i ); nextChopPoint = chopPoints.get( i + 1 ); while ( nextChopPoint != null && ( nextChopPoint[ 0 ] <= thisChopPoint[ 1 ] ) ) { if ( nextChopPoint[ 1 ] >= thisChopPoint[ 1 ] ) { thisChopPoint[ 1 ] = nextChopPoint[ 1 ]; } chopPoints.remove( i + 1 ); if ( i < chopPoints.size() - 1 ) { nextChopPoint = chopPoints.get( i + 1 ); } else { nextChopPoint = null; } } } } // interface ChopPointComparator<int[]> implements Comparator<int[]> { // public int compare( int[] left, int[] right ) { // if ( left[ 0 ] == right[ 0 ] ) { return ( left[ 1 ] - right[ 1 ] ); } // return ( left[ 0 ] - right[ 0 ] ); // } // } } As written, this class works by instantiating a new anonymous inner Comparator class each time a search is performed. I would prefer the class had a static inner class to avoid the overhead of object instantiation each time a search is performed. However, if you comment out the indicated method and uncomment the lines that are currently commented, the code no longer compiles. I would greatly appreciate some ideas as to why. Thanks in advance for your help, John
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
Did you try making it a static class instead of an interface? An interface can't implement an interface.
|
 |
Fritz Todd
Greenhorn
Joined: Apr 13, 2007
Posts: 7
|
|
The same thing happens whether I make it an interface or a static class. Thanks, John
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
It only takes a few microseconds to create an object, so it really isn't worth worrying about that. But if you really want only one of those comparators, then what you're looking for is a static variable. Like this declaration:Note, that's a declaration. Don't put it inside the method where you have the code now.
|
 |
Fritz Todd
Greenhorn
Joined: Apr 13, 2007
Posts: 7
|
|
Thanks for the help. Making the Comparator a static variable declaration worked. I have further simplified the example and post here a version that works as intended: Thanks again, John
|
 |
 |
|
|
subject: Generics, arrays, and sorting
|
|
|