Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
vsh
New Contributor
Posts: 5
Registered: ‎10-05-2010
My Device: BB8820

BigVector firstIndexOf

Hello There Smiley Happy

 

Need a little help here. I am storing Strings in BigVector because I needed the functions provided i.e. firstIndexOf, binarySearch etc.

Now, I should have probably done a small sample program before going ahead with actually implementing it.

But, turns out, I wasnt thinking right then.

So, currently i find I'm unable to see results as per my inference i.e. firstIndexOf function returning -1 incase the String doesnt exist in the BigVector.

Sample program below:

BigVector vcVals= new BigVector();
vcVals.addElement(new String("ABC"));
vcVals.addElement(new String("ABC1"));
vcVals.addElement(new String("ABC2"));
vcVals.addElement(new String("ABC3"));
//Not adding ABC4		
vcVals.addElement(new String("ABC5"));
vcVals.addElement(new String("ABC6"));

System.out.println("ABC3 find int value = "+vcVals.firstIndexOf("ABC3"));
System.out.println("ABC4 find int value = "+vcVals.firstIndexOf("ABC4"));

 

 I was expecting

ABC3 find int value = 3

ABC4 find int value = -1

But I get

ABC3 find int value = -1

ABC4 find int value = -1

 

I'm sure the problem lies with my inference of how this function would work. Can someone help me out as I want to avoid the "equals" operation which is expensive.

 

JDE version- 5.0

http://www.blackberry.com/developers/docs/4.2.1api/net/rim/device/api/collection/util/BigVector.html

Excerpt for firstIndexOf

firstIndexOf

public int firstIndexOf(Object value)

Finds first instance of value in list.

Note: this method does not use Object.equals(java.lang.Object) for comparison, since that would be too expensive. This method is still expensive, though, and you should use it carefully and sparingly.

Parameters: value- Value to search for. Returns: Index of first instance of your value, or -1 if the value does not exist in this vector.


Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: BigVector firstIndexOf

My guess is that the problem is caused by the "new String("...");" usage.

 

Try using the getIndex method.

 

You would have to implement your own String comparator but that rather simple since you can use the String.compareTo function.

 

E.

Developer
Posts: 16,838
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: BigVector firstIndexOf

"ABC" already creates a String object, using new String in addition is one object too much in any case.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter