You can't define a variable-size array,
java arrays are fixed length. However, using your recordsFound array as temporary storage (initialised to the full length of your collection), you can fill it from position 0 up and keep track of how many array elements are valid. Then all you have to do is copy the valid bit over into a new array.
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];
}
(edit) I had to remove the [code../code] block because it ate my boldface highlights.
(edit #2) The forum engine ate my explanation, AND there was a bug in my code snippet. Note that the
if (lastValidIndex > -1) condition isn't even necessary (code creates a 0-length array and copies 0 elements into the new array if it's not there), but I wrote it out explicitly for clarity or in case you have to throw an exception or something like that.
Hope this helps
[ July 24, 2005: Message edited by: Barend Garvelink ]