int[] recordsFound = new int[ classSize ];
int lastValidIndex = -1;
int y = 0;
for( int x = 0; x < classSize; x++ )
{
String[] record = read( x );
if( record[0].startsWith( criteria[0] )
&& record[1].startsWith( criteria[2] ) )
{
recordsFound[ ++lastValidIndex ] = x;
}
}
int[] finalResult;
if (lastvalidIndex > -1) {
finalResult = new int[lastValidIndex + 1];
System.arraycopy(recordsFound, 0, finalResult, 0, finalResult.length);
}
else
{
finalResult = new int[0];
}
SCJP 1.4, SCJD 1.4, SCWCD 1.3, ICSD:Websphere 5.1
Well unfortunately, I cannot use an ArrayList.Got any other ideas.
Originally posted by Mark Wuest:
Mark
SCJP 1.4, SCJD 1.4, SCWCD 1.3, ICSD:Websphere 5.1
Originally posted by Barend Garvelink:
Mark<hr></blockquote>
Mark,
Two things:
1) this code will throw an ArrayIndexOutOfBounds exception courtesy of the less-than-or-equal operator you used in the middle section of the for-loop.
2) for every element in the loop, this code will do the "array index within bounds" checking. If you use System.arraycopy(Object,int,Object,int,int) instead, the JVM will perform bounds-checking only once.[/QB]
Originally posted by Mark Wuest:
I think we did their homework assignment for them.![]()
SCJP 1.4, SCJD 1.4, SCWCD 1.3, ICSD:Websphere 5.1
Consider Paul's rocket mass heater. |