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

S

Stalin

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?

Thanks
 
R

Roedy Green

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?

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
http://mindprod.com/jgloss/hashmap.html


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
value.

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

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

String value = map.get( "LastName");
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top