More ArrayList utility methods

Discussion in 'Java' started by Roedy Green, Sep 8, 2003.

  1. Roedy Green

    Roedy Green Guest

    /**
    * 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



    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 8, 2003
    #1
    1. Advertising

  2. Roedy Green

    Ike Guest

    Fantastic...going to put these up on your sight as well? -Ike
    Ike, Sep 8, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,737
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,360
    Kaidi
    Jan 3, 2004
  3. xz
    Replies:
    16
    Views:
    2,363
  4. Philipp
    Replies:
    6
    Views:
    920
    Arne Vajhøj
    May 28, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    311
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page