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
Trusted Contributor
Posts: 138
Registered: ‎07-11-2010
My Device: Bold 9700
My Carrier: O2
Accepted Solution

convert vector to array

since the Vector class provided doesn't have a toArray() method, can anyone recommend a way to convert a vector to an array? (note: i can't just use the toString() method and then split the vector using the delimeter ',' since the vector itself contains strings which may contain commas).

Trusted Contributor
Posts: 138
Registered: ‎07-11-2010
My Device: Bold 9700
My Carrier: O2

Re: convert vector to array

nevermind. this is blindingly obvious.

Developer
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: convert vector to array

You will have to write a utility method. Best place is to it in a utility class and make the method public static, so you can use it wherever you need to.

 

The method could look like:

 

public static void copyVectorToArray(Vector src, Object[] target) {

 

   if(target.length != src.size() ) {

      throw new IllegalArgumentException("Target array length must be size of src vector");

   }

 

   Enumeration enum = src.elements();

   int count = 0;

   while(enum.hasMoreElements()) {

      Object element = enum.nextElement();

      target[count++] = element;

    }

 

}

 

And then you call it like

 

String[] myStringArray = new String[myStringVector.size()];

copyVectorToArray(myStringVector, myStringArray);

 

 

Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: convert vector to array

@pfiffi78: Or you can just call the copyInto function thats built into Vector.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Highlighted
Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: convert vector to array

The Vector class  has a method to do that :

 

String[] myStringArray = new String[myStringVector.size()];
myStringVector.copyInto(myStringArray));