I am doing a method that needs to return a long[]. It looks like this:
public long[] parseString(String input)
input are strings like:
Inside parseString I use a regex to get all numbers and add them to an ArrayList as I can't know how many oconcurrences it will find.
At the end I create a long[] with the size of the arrayList and do a for each to add it to the long[] var.
Another way would be: First count every occurrence with a
while ( matcher.find() ) size++;
and then with size create a long[] of size size and do a: matcher.reset() and now save the long values in the long[] variable.
Which do you think it's the best?
Is there a better way to do this?
Remember I can't change the method signature :(
At the risk of being a huge pimp for Google's guava-libraries (I really do love it!), the Longs class makes this a one-liner:
return Longs.toArray(foundLongs);
ta-daa!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With