More ArrayList utility methods

R

Roedy Green

/**
* Extract a range of a an ArrayList
* Finds end points by binary search.
*
* @param a ArrayList to extract the range from.
*
* @param comparator comparator Comparator for comparing two
elements in the ArrayLists
* @param from lowest object to return. Need not exactually
exist.
*
* @param to Highest object to return, need not actually
exist.
* @param presorted true if the array has already been sorted.
* false, the array will be sorted first.
*
* @return subrange of the array starting with first element
matching from,
* and ending with last element matching to.
*/
public static ArrayList range ( ArrayList a, Comparator comparator,
Object from, Object to, boolean presorted )
{
int size = a.size();
if ( ! presorted )
{
Collections.sort( a, comparator );
}
// find low element.
int fromIndex = Collections.binarySearch( a, from, comparator );
if ( fromIndex < 0 )
{
fromIndex = -(fromIndex+1);
}
else
{
// if there are dups, no guarantee we hit the first one.
// Back up till hit something
// too small
for ( int i=fromIndex-1; i >= 0; i-- )
{
if ( comparator.compare( a.get ( i ), from ) < 0 )
{
// we went too far.
break;
}
else
{
fromIndex = i;
}
} // end for
} // end else

// find high element.
int toIndex = Collections.binarySearch( a, to, comparator );
if ( toIndex < 0 )
{
toIndex = -(toIndex+1)-1;
}
else
{
// if there are dups, no guarantee we hit the first one.
// look ahead till hit something
// too big
for ( int i=toIndex+1; i<size; i++ )
{
if ( comparator.compare( a.get ( i ), to ) > 0 )
{
// we went too far.
break;
}
else
{
toIndex = i;
}
} // end for
} // end else

if ( fromIndex <= toIndex )
{
int resultSize = toIndex-fromIndex + 1;
ArrayList result = new ArrayList ( resultSize );

for ( int i=fromIndex; i<=toIndex; i++ )
{
result.add( a.get(i) );
}
return result;
}
else
{
return new ArrayList( 0 );
}

}

/**
* Prune out any null elements from the array.
*
* @param a ArrayList to be pruned of null elements.
*
* @return ArrayList with null elements removed.
*/
public static ArrayList pruneNulls( ArrayList a )
{
// we don't both with two passes to get a perfectly accurate
size estimate.
int size = a.size();
ArrayList result = new ArrayList( size );

for ( int i=0; i<size; i++ )
{
Object elt = a.get( i );
if ( elt != null )
{
result.add ( elt );
}
}
return result;
}
/**
* test harness
*
* @param args not used
*/
public static void main ( String[] args )
{
if ( DEBUG )
{

// test range

{
ArrayList primary = new ArrayList();
primary.add( "apple" );
primary.add( "apricot" );
primary.add( "apricot" );
primary.add( "apricot" );
primary.add( "apricot" );
primary.add( "GRAPEFRUIT" );
primary.add( "grapefruit");
primary.add( "lemon" );
primary.add( "lemon" );
primary.add( "peach" );
primary.add( "pear" );
primary.add( "strawberry" );

ArrayList r = range ( primary,
String.CASE_INSENSITIVE_ORDER, "apricot", "lemon", false );
int size = r.size();
System.out.println( "range --------------" );
for ( int i=0; i<size; i++ )
{
System.out.println( r.get(i) );
}

}
// test pruneNulls

{
ArrayList primary = new ArrayList();
primary.add( "apple" );
primary.add( "apricot" );
primary.add( "apricot" );
primary.add( "apricot" );
primary.add( "apricot" );
primary.add( "GRAPEFRUIT" );
primary.add( "grapefruit");
primary.add( "lemon" );
primary.add( "lemon" );
primary.add( "peach" );
primary.add( "pear" );
primary.add( "strawberry" );
primary.set( 2, null );
primary.set( 6, null );

ArrayList r = pruneNulls ( primary);
int size = r.size();
System.out.println( "pruneNulls --------------" );
for ( int i=0; i<size; i++ )
{
System.out.println( r.get(i) );
}
}
} // end if DEBUG
} // end main
} // end Merge
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top