I’m trying to examine an array of strings and find out which is the widest. Here’s an approximation of the code I’m using:
// The set of texts:
String [] texts = a bunch of texts
Font [] fonts = the fonts of those texts
// A variable to hold the longest line, initially set to the width of the first line:
double longestLine = ( the width of the first line, calculated using the font and the getStringBounds method) ;
// A variable to hold the current width when counting through the texts:
double currentWidth ;
// Now, counting through the texts to find the longest one:
for ( int i=1 ; i < texts.length ; i ++ ) {
currentWidth = ( again using getStringBounds, this time to find the width of the current line ) ;
if ( longestLine < currentWidth )
longestLine = new Double ( currentWidth ).doubleValue () ;
}
So, here’s my assignment question. What I really want to do in that last line is:
longestLine = currentWidth ;
except I suspect that will make longestLine and currentWidth both pointers to the same variable, and thus longestLine will change whenever currentWidth changes. That’s not what I want. But using “new Double ( currentWidth ).doubleValue ()” seems like a really dumb way to do things. I mean, really: creating a Double from a double so that I can get a new double? That seems really roundabout. Is there a simpler way to do the same thing?
