Arrays.binarySearch(Object[], keyValue) and Reflection API

Discussion in 'Java' started by Stalin, Sep 29, 2005.

  1. Stalin

    Stalin Guest

    Hi guys,
    public Objectbinary(Object[], String keyName, String keyValue){

    int index = binarySearch(Object[],????);
    return Object[index];
    To make this method work I have to give it array and object that it will be
    looking for. The thing is I don't know what kind of object in Object [] so I
    have construct it during runtime by only have keyName (Ex. "LastName" ) and
    keyValue (Ex "Robinson").

    I've been deeging Reflection API last couple of days but can't figure this
    one out.

    Anybody have an idea how to do it?

    Stalin, Sep 29, 2005
    1. Advertisements

  2. Stalin

    Roedy Green Guest

    I suspect you may not need reflection at all.

    All you need as a class called KV with two String fields key and
    value. You would implement a hashCode and equals method. See

    You then can say

    map .add ( "LastName", new KV( "LastName", "Robinson" ) )

    Or maybe you don't even need that just use String as both key and

    HashMap<String,String> map = new HashMap<String,String>( 149 );

    map.put ( "LastName", "Robinson" );

    String value = map.get( "LastName");
    Roedy Green, Sep 29, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.