This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Generics, arrays, and sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Generics, arrays, and sorting" Watch "Generics, arrays, and sorting" New topic
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
    
    2

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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Generics, arrays, and sorting
 
Similar Threads
Doubt abt Inner class using with Collections.
ClasscastException in Collections.sort()
Sorting ArrayList
Generic query - wildcards
High Scores Table