Getting an int when using Comparable object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Getting an int when using Comparable object

  1. #1
    Join Date
    Feb 2005
    Posts
    22

    Getting an int when using Comparable object

    Trying to get an understanding of the binary search-ultimately I want to return the index where a new item would be inserted in the array. Taking small steps, I thought I would first see if I could get the item found index out of the array; meaning if the item is found it would print out the index position. However, when using Comparable it seems more of a challenge because int is a primative type. So far, i've managed to figure out that the
    Integer indexOf = new Integer( 5 ); really isn't doing anything.
    Any suggestions?

    import java.lang.*;

    public class BinaryIndex{

    public static void main ( String args[] ){

    String objArray[] = { "a", "c", "e", "f", "g", "h", "i" };
    String searchObj = "c";
    binarySearch( objArray, searchObj);
    System.out.println("Return the index of the insert point." );

    System.out.println( "End of program" );
    }

    public static int binarySearch( Comparable[] objArray, Comparable searchObj )
    {
    int low = 0;
    int high = objArray.length - 1;
    int mid = 0;
    Integer indexOf = new Integer( 5 );

    while (low <= high )
    {
    mid = ( low + high ) / 2;
    if (objArray[mid].compareTo( searchObj) < 0 )
    low = mid + 1;
    else if ( objArray[mid].compareTo( searchObj ) > 0 )
    high = mid - 1;
    else
    return mid;
    System.out.println( indexOf.intValue());

    }
    // item not found
    System.out.println( "Item Not Found" );
    return -1;
    }

    }

  2. #2
    Join Date
    Feb 2005
    Posts
    22
    Actually, I figured this one out myself. I got rid of
    Integer indexOf = new Integer( 5 ); -- and --
    System.out.println( indexOf.intValue()); since they were useless
    Then in my main method changed
    binarySearch( objArray, searchObj); -- to --
    int insertPoint = binarySearch( objArray, searchObj);

    and viola

Similar Threads

  1. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 02:32 PM
  2. Object Array ? Do you Worth it
    By ashish in forum Java
    Replies: 1
    Last Post: 05-23-2002, 07:50 AM
  3. Producer Consumer Thread
    By Chirag in forum Java
    Replies: 6
    Last Post: 04-10-2002, 05:22 AM
  4. INSERT for multiple tables with IDENTITY
    By Kay in forum Database
    Replies: 1
    Last Post: 07-04-2001, 05:18 AM
  5. Re: COM object Password Security
    By Tom Shreve in forum Enterprise
    Replies: 0
    Last Post: 04-07-2000, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center